In [1]:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import warnings
import os
import tensorflow as tf
warnings.filterwarnings('ignore')
import shutil
from random import shuffle
Unzipping Data¶
In [2]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/archive_1.zip
!unzip -qo archive_1.zip
# zip folder is not needed anymore
!rm archive_1.zip
In [3]:
shapes = ['triangles', 'circles', 'squares']
path= 'shapes/'
files= []
result= []
for shape in shapes:
new_path= path+shape
for file in os.listdir(new_path):
files.append(os.path.join(new_path,file))
result.append(shape)
In [4]:
len(files)
Out[4]:
In [5]:
len(result)
Out[5]:
In [6]:
files[:5]
Out[6]:
In [7]:
from skimage import color
images=[]
for file in files:
img= plt.imread(file)
img= color.rgb2gray(img)
img= img.ravel()
images.append(img)
In [8]:
len(images)
Out[8]:
Data Visualsation¶
In [9]:
df= pd.DataFrame(images)
In [10]:
df.head()
Out[10]:
In [11]:
df['result']= result
df.head()
Out[11]:
In [12]:
from sklearn.utils import shuffle
df= shuffle(df)
df.head()
Out[12]:
In [13]:
df['result']= df['result'].replace({'circles':0, 'triangles':1, 'squares':2})
df.head()
Out[13]:
Dropping useless column¶
In [14]:
y= df['result']
X= df.drop('result', axis=1)
In [15]:
X= X.values.reshape(-1,28,28,1)
Uniform Type Conversion¶
In [16]:
from tensorflow.keras.utils import to_categorical
y= to_categorical(y)
y.astype('int32')
Out[16]:
Train Test Split¶
In [17]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test= train_test_split(X,y, test_size=0.2, random_state=42)
Model Architecture¶
In [18]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, BatchNormalization, MaxPool2D
In [19]:
model= Sequential()
model.add(Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(28,28,1)))
model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(3, activation='softmax'))
In [20]:
model.summary()
In [21]:
from tensorflow.keras.callbacks import ModelCheckpoint
model_save= ModelCheckpoint('best.h5', save_best_only=True, monitor='val_accuracy')
In [22]:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Training the model¶
In [23]:
history= model.fit(X_train, y_train, batch_size=20, epochs=100, verbose=1, validation_data=(X_test, y_test), callbacks=[model_save])
Result and Prediction¶
In [26]:
model.load_weights('best.h5')
model.evaluate(X_test,y_test, batch_size=20)
Out[26]:
In [27]:
pred= model.predict(X_test)
predictions= np.argmax(pred, axis=1)
predictions
Out[27]:
Accuracy and Loss Graph¶
In [28]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
Out[28]:
In [29]:
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
Out[29]:
Testing¶
In [30]:
plt.imshow(X_test[2])
Out[30]:
In [31]:
y_train[2]
Out[31]:
In [32]:
predictions[2]
Out[32]:
DeepCC¶
In [33]:
!deepCC best.h5