#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  8 12:11:48 2018

@author: dhruvkapur
"""
import tensorflow,keras
from keras.datasets import fashion_mnist
from sklearn.model_selection import train_test_split
(train_X,train_Y),(test_X,test_Y) = fashion_mnist.load_data()
import numpy as np
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from keras.models import Sequential,Input,Model
from keras.layers import Dense,Dropout,Flatten
from keras.layers import Conv2D,MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
classes = np.unique(train_Y)
nclasses = len(classes)
plt.figure(figsize=[5,5])
plt.subplot(121)
plt.imshow(train_X[0,:,:], cmap='gray')
plt.subplot(122)
plt.imshow(test_X[0,:,:], cmap='gray')
train_X = train_X.reshape(-1,28,28,1)
test_X=test_X.reshape(-1,28,28,1)
train_X=train_X.astype('float32')
test_X=test_X.astype('float32')
train_X = train_X/255.
test_X = test_X/255.
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
train_Y_one_hot = to_categorical(train_Y)
train_X,valid_X,train_label,valid_label = train_test_split(train_X,train_Y_one_hot,train_size = 0.8)

batch_size=64
epochs = 1
num_classes=10
fashion_model = Sequential()
fashion_model.add(Conv2D(32,kernel_size=(3,3),activation='linear',input_shape=(28,28,1),padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Conv2D(64,(3,3),activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Conv2D(128,(3,3),activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128,activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(Dense(num_classes,activation='softmax'))
fashion_model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adam(lr=0.01),
                      metrics=['accuracy'])
fashion_model.summary()
fashion_train=fashion_model.fit(train_X,train_label,batch_size=batch_size,epochs=epochs)
test_eval=fashion_model.evaluate(test_X,test_Y_one_hot,verbose=1)
loss=(test_eval[0])
accuracy=(test_eval[1])

predicted_classes = fashion_model.predict(test_X)
predicted_classes = np.argmax(np.round(predicted_classes),axis=1)
correct = np.where(predicted_classes==test_Y)[0]
print ("Found %d correct labels" % len(correct))
for i, correct in enumerate(correct[:9]):
    plt.subplot(3,3,i+1)
    plt.imshow(test_X[correct].reshape(28,28), cmap='gray', interpolation='none')
    plt.title("Predicted {}, Class {}".format(predicted_classes[correct], test_Y[correct]))
    #plt.tight_layout()
    
incorrect = np.where(predicted_classes!=test_Y)[0]
print ("Found %d incorrect labels" % len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
    plt.subplot(3,3,i+1)
    plt.imshow(test_X[incorrect].reshape(28,28), cmap='gray', interpolation='none')
    plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], test_Y[incorrect]))
    #plt.tight_layout()













bd02ea3fc91904434e22b73c9db6b6db346006a5
<mask token>

<mask token>
plt.figure(figsize=[5, 5])
plt.subplot(121)
plt.imshow(train_X[0, :, :], cmap='gray')
plt.subplot(122)
plt.imshow(test_X[0, :, :], cmap='gray')
<mask token>
fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear',
    input_shape=(28, 28, 1), padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128, activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(Dense(num_classes, activation='softmax'))
fashion_model.compile(loss=keras.losses.categorical_crossentropy, optimizer
    =keras.optimizers.Adam(lr=0.01), metrics=['accuracy'])
fashion_model.summary()
<mask token>
print('Found %d correct labels' % len(correct))
for i, correct in enumerate(correct[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[correct].reshape(28, 28), cmap='gray', interpolation=
        'none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[correct],
        test_Y[correct]))
<mask token>
print('Found %d incorrect labels' % len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[incorrect].reshape(28, 28), cmap='gray',
        interpolation='none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[incorrect],
        test_Y[incorrect]))

<mask token>
(train_X, train_Y), (test_X, test_Y) = fashion_mnist.load_data()
<mask token>
classes = np.unique(train_Y)
nclasses = len(classes)
plt.figure(figsize=[5, 5])
plt.subplot(121)
plt.imshow(train_X[0, :, :], cmap='gray')
plt.subplot(122)
plt.imshow(test_X[0, :, :], cmap='gray')
train_X = train_X.reshape(-1, 28, 28, 1)
test_X = test_X.reshape(-1, 28, 28, 1)
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X / 255.0
test_X = test_X / 255.0
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
train_Y_one_hot = to_categorical(train_Y)
train_X, valid_X, train_label, valid_label = train_test_split(train_X,
    train_Y_one_hot, train_size=0.8)
batch_size = 64
epochs = 1
num_classes = 10
fashion_model = Sequential()
fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear',
    input_shape=(28, 28, 1), padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128, activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(Dense(num_classes, activation='softmax'))
fashion_model.compile(loss=keras.losses.categorical_crossentropy, optimizer
    =keras.optimizers.Adam(lr=0.01), metrics=['accuracy'])
fashion_model.summary()
fashion_train = fashion_model.fit(train_X, train_label, batch_size=
    batch_size, epochs=epochs)
test_eval = fashion_model.evaluate(test_X, test_Y_one_hot, verbose=1)
loss = test_eval[0]
accuracy = test_eval[1]
predicted_classes = fashion_model.predict(test_X)
predicted_classes = np.argmax(np.round(predicted_classes), axis=1)
correct = np.where(predicted_classes == test_Y)[0]
print('Found %d correct labels' % len(correct))
for i, correct in enumerate(correct[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[correct].reshape(28, 28), cmap='gray', interpolation=
        'none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[correct],
        test_Y[correct]))
incorrect = np.where(predicted_classes != test_Y)[0]
print('Found %d incorrect labels' % len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[incorrect].reshape(28, 28), cmap='gray',
        interpolation='none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[incorrect],
        test_Y[incorrect]))

<mask token>
import tensorflow, keras
from keras.datasets import fashion_mnist
from sklearn.model_selection import train_test_split
(train_X, train_Y), (test_X, test_Y) = fashion_mnist.load_data()
import numpy as np
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from keras.models import Sequential, Input, Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
classes = np.unique(train_Y)
nclasses = len(classes)
plt.figure(figsize=[5, 5])
plt.subplot(121)
plt.imshow(train_X[0, :, :], cmap='gray')
plt.subplot(122)
plt.imshow(test_X[0, :, :], cmap='gray')
train_X = train_X.reshape(-1, 28, 28, 1)
test_X = test_X.reshape(-1, 28, 28, 1)
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X / 255.0
test_X = test_X / 255.0
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
train_Y_one_hot = to_categorical(train_Y)
train_X, valid_X, train_label, valid_label = train_test_split(train_X,
    train_Y_one_hot, train_size=0.8)
batch_size = 64
epochs = 1
num_classes = 10
fashion_model = Sequential()
fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear',
    input_shape=(28, 28, 1), padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2), padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128, activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(Dense(num_classes, activation='softmax'))
fashion_model.compile(loss=keras.losses.categorical_crossentropy, optimizer
    =keras.optimizers.Adam(lr=0.01), metrics=['accuracy'])
fashion_model.summary()
fashion_train = fashion_model.fit(train_X, train_label, batch_size=
    batch_size, epochs=epochs)
test_eval = fashion_model.evaluate(test_X, test_Y_one_hot, verbose=1)
loss = test_eval[0]
accuracy = test_eval[1]
predicted_classes = fashion_model.predict(test_X)
predicted_classes = np.argmax(np.round(predicted_classes), axis=1)
correct = np.where(predicted_classes == test_Y)[0]
print('Found %d correct labels' % len(correct))
for i, correct in enumerate(correct[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[correct].reshape(28, 28), cmap='gray', interpolation=
        'none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[correct],
        test_Y[correct]))
incorrect = np.where(predicted_classes != test_Y)[0]
print('Found %d incorrect labels' % len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
    plt.subplot(3, 3, i + 1)
    plt.imshow(test_X[incorrect].reshape(28, 28), cmap='gray',
        interpolation='none')
    plt.title('Predicted {}, Class {}'.format(predicted_classes[incorrect],
        test_Y[incorrect]))

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  8 12:11:48 2018

@author: dhruvkapur
"""
import tensorflow,keras
from keras.datasets import fashion_mnist
from sklearn.model_selection import train_test_split
(train_X,train_Y),(test_X,test_Y) = fashion_mnist.load_data()
import numpy as np
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from keras.models import Sequential,Input,Model
from keras.layers import Dense,Dropout,Flatten
from keras.layers import Conv2D,MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
classes = np.unique(train_Y)
nclasses = len(classes)
plt.figure(figsize=[5,5])
plt.subplot(121)
plt.imshow(train_X[0,:,:], cmap='gray')
plt.subplot(122)
plt.imshow(test_X[0,:,:], cmap='gray')
train_X = train_X.reshape(-1,28,28,1)
test_X=test_X.reshape(-1,28,28,1)
train_X=train_X.astype('float32')
test_X=test_X.astype('float32')
train_X = train_X/255.
test_X = test_X/255.
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
train_Y_one_hot = to_categorical(train_Y)
train_X,valid_X,train_label,valid_label = train_test_split(train_X,train_Y_one_hot,train_size = 0.8)

batch_size=64
epochs = 1
num_classes=10
fashion_model = Sequential()
fashion_model.add(Conv2D(32,kernel_size=(3,3),activation='linear',input_shape=(28,28,1),padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Conv2D(64,(3,3),activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Conv2D(128,(3,3),activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2,2),padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128,activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(Dense(num_classes,activation='softmax'))
fashion_model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adam(lr=0.01),
                      metrics=['accuracy'])
fashion_model.summary()
fashion_train=fashion_model.fit(train_X,train_label,batch_size=batch_size,epochs=epochs)
test_eval=fashion_model.evaluate(test_X,test_Y_one_hot,verbose=1)
loss=(test_eval[0])
accuracy=(test_eval[1])

predicted_classes = fashion_model.predict(test_X)
predicted_classes = np.argmax(np.round(predicted_classes),axis=1)
correct = np.where(predicted_classes==test_Y)[0]
print ("Found %d correct labels" % len(correct))
for i, correct in enumerate(correct[:9]):
    plt.subplot(3,3,i+1)
    plt.imshow(test_X[correct].reshape(28,28), cmap='gray', interpolation='none')
    plt.title("Predicted {}, Class {}".format(predicted_classes[correct], test_Y[correct]))
    #plt.tight_layout()
    
incorrect = np.where(predicted_classes!=test_Y)[0]
print ("Found %d incorrect labels" % len(incorrect))
for i, incorrect in enumerate(incorrect[:9]):
    plt.subplot(3,3,i+1)
    plt.imshow(test_X[incorrect].reshape(28,28), cmap='gray', interpolation='none')
    plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], test_Y[incorrect]))
    #plt.tight_layout()












