Cainvas
In [27]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/mobilenet_v1.zip -O mobilenet_v1.zip
--2021-06-26 08:37:26--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/mobilenet_v1.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.36
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.36|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9263 (9.0K) [application/x-zip-compressed]
Saving to: ‘mobilenet_v1.zip’

mobilenet_v1.zip    100%[===================>]   9.05K  --.-KB/s    in 0s      

2021-06-26 08:37:26 (196 MB/s) - ‘mobilenet_v1.zip’ saved [9263/9263]

In [28]:
import zipfile as zf
files = zf.ZipFile("mobilenet_v1.zip", 'r')
files.extractall('mobilenet_v1')
files.close()
In [29]:
from tensorflow import keras
import numpy as np
from tensorflow.keras import backend as K
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications import imagenet_utils
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout,Flatten
import matplotlib.pyplot as plt
import cv2

from mobilenet_v1.mobilenet import MobileNet

from tensorflow.keras.applications.mobilenet import preprocess_input
In [30]:
# the parameters
IMAGE_SIZE = 224
ALPHA=0.75
EPOCHS=20
In [31]:
def prepare_image(file):
    img_path = ''
    img = image.load_img(img_path + file, target_size=(IMAGE_SIZE, IMAGE_SIZE))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)
In [32]:
# function to define dropout, hidden layers and the number of output
def build_finetune_model(base_model, dropout, fc_layers, num_classes):
    for layer in base_model.layers:
        layer.trainable = False

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    
    for fc in fc_layers:
        # New FC layer, random init
        x = Dense(fc, activation='relu')(x) 
        x = Dropout(dropout)(x)

    # New softmax layer
    predictions = Dense(num_classes, activation='softmax')(x) 
    
    finetune_model = Model(inputs=base_model.input, outputs=predictions)

    return finetune_model
In [33]:
# Using MobileNetv1
base_model=MobileNet(input_shape=(IMAGE_SIZE, IMAGE_SIZE,3), alpha = ALPHA, 
                     depth_multiplier = 1, dropout = 0.001, include_top = False, 
                     weights = "imagenet", classes = 4, backend=keras.backend,
                     layers=keras.layers,models=keras.models,utils=keras.utils)
In [34]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/3D_Shapes.zip -O 3D_Shapes.zip
--2021-06-26 08:37:41--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/3D_Shapes.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.66.56
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.66.56|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2137601 (2.0M) [application/x-zip-compressed]
Saving to: ‘3D_Shapes.zip’

3D_Shapes.zip       100%[===================>]   2.04M  --.-KB/s    in 0.03s   

2021-06-26 08:37:41 (80.1 MB/s) - ‘3D_Shapes.zip’ saved [2137601/2137601]

In [35]:
import zipfile as zf
files = zf.ZipFile("3D_Shapes.zip", 'r')
files.extractall('3D_Shapes_Dataset')
files.close()
In [36]:
FC_LAYERS = [100, 50]
dropout = 0.5

finetune_model = build_finetune_model(base_model, 
                                      dropout=dropout, 
                                      fc_layers=FC_LAYERS, 
                                      num_classes=4)
In [37]:
train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator=train_datagen.flow_from_directory('3D_Shapes_Dataset',
                                                 target_size=(IMAGE_SIZE,IMAGE_SIZE),
                                                 color_mode='rgb',
                                                 batch_size=32,
                                                 class_mode='categorical', shuffle=True)
Found 1182 images belonging to 4 classes.
In [38]:
finetune_model.summary()
finetune_model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])
step_size_train=train_generator.n//train_generator.batch_size
history = finetune_model.fit_generator(generator=train_generator,steps_per_epoch=step_size_train,epochs=EPOCHS, shuffle=True)

finetune_model.save('shape_model.h5')
Model: "functional_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 226, 226, 3)       0         
_________________________________________________________________
conv1 (Conv2D)               (None, 112, 112, 24)      648       
_________________________________________________________________
conv1_bn (BatchNormalization (None, 112, 112, 24)      96        
_________________________________________________________________
conv1_relu (ReLU)            (None, 112, 112, 24)      0         
_________________________________________________________________
conv_dw_1 (DepthwiseConv2D)  (None, 112, 112, 24)      216       
_________________________________________________________________
conv_dw_1_bn (BatchNormaliza (None, 112, 112, 24)      96        
_________________________________________________________________
conv_dw_1_relu (ReLU)        (None, 112, 112, 24)      0         
_________________________________________________________________
conv_pw_1 (Conv2D)           (None, 112, 112, 48)      1152      
_________________________________________________________________
conv_pw_1_bn (BatchNormaliza (None, 112, 112, 48)      192       
_________________________________________________________________
conv_pw_1_relu (ReLU)        (None, 112, 112, 48)      0         
_________________________________________________________________
conv_pad_2 (ZeroPadding2D)   (None, 114, 114, 48)      0         
_________________________________________________________________
conv_dw_2 (DepthwiseConv2D)  (None, 56, 56, 48)        432       
_________________________________________________________________
conv_dw_2_bn (BatchNormaliza (None, 56, 56, 48)        192       
_________________________________________________________________
conv_dw_2_relu (ReLU)        (None, 56, 56, 48)        0         
_________________________________________________________________
conv_pw_2 (Conv2D)           (None, 56, 56, 96)        4608      
_________________________________________________________________
conv_pw_2_bn (BatchNormaliza (None, 56, 56, 96)        384       
_________________________________________________________________
conv_pw_2_relu (ReLU)        (None, 56, 56, 96)        0         
_________________________________________________________________
conv_dw_3 (DepthwiseConv2D)  (None, 56, 56, 96)        864       
_________________________________________________________________
conv_dw_3_bn (BatchNormaliza (None, 56, 56, 96)        384       
_________________________________________________________________
conv_dw_3_relu (ReLU)        (None, 56, 56, 96)        0         
_________________________________________________________________
conv_pw_3 (Conv2D)           (None, 56, 56, 96)        9216      
_________________________________________________________________
conv_pw_3_bn (BatchNormaliza (None, 56, 56, 96)        384       
_________________________________________________________________
conv_pw_3_relu (ReLU)        (None, 56, 56, 96)        0         
_________________________________________________________________
conv_pad_4 (ZeroPadding2D)   (None, 58, 58, 96)        0         
_________________________________________________________________
conv_dw_4 (DepthwiseConv2D)  (None, 28, 28, 96)        864       
_________________________________________________________________
conv_dw_4_bn (BatchNormaliza (None, 28, 28, 96)        384       
_________________________________________________________________
conv_dw_4_relu (ReLU)        (None, 28, 28, 96)        0         
_________________________________________________________________
conv_pw_4 (Conv2D)           (None, 28, 28, 192)       18432     
_________________________________________________________________
conv_pw_4_bn (BatchNormaliza (None, 28, 28, 192)       768       
_________________________________________________________________
conv_pw_4_relu (ReLU)        (None, 28, 28, 192)       0         
_________________________________________________________________
conv_dw_5 (DepthwiseConv2D)  (None, 28, 28, 192)       1728      
_________________________________________________________________
conv_dw_5_bn (BatchNormaliza (None, 28, 28, 192)       768       
_________________________________________________________________
conv_dw_5_relu (ReLU)        (None, 28, 28, 192)       0         
_________________________________________________________________
conv_pw_5 (Conv2D)           (None, 28, 28, 192)       36864     
_________________________________________________________________
conv_pw_5_bn (BatchNormaliza (None, 28, 28, 192)       768       
_________________________________________________________________
conv_pw_5_relu (ReLU)        (None, 28, 28, 192)       0         
_________________________________________________________________
conv_pad_6 (ZeroPadding2D)   (None, 30, 30, 192)       0         
_________________________________________________________________
conv_dw_6 (DepthwiseConv2D)  (None, 14, 14, 192)       1728      
_________________________________________________________________
conv_dw_6_bn (BatchNormaliza (None, 14, 14, 192)       768       
_________________________________________________________________
conv_dw_6_relu (ReLU)        (None, 14, 14, 192)       0         
_________________________________________________________________
conv_pw_6 (Conv2D)           (None, 14, 14, 384)       73728     
_________________________________________________________________
conv_pw_6_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_6_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_dw_7 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      
_________________________________________________________________
conv_dw_7_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_dw_7_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pw_7 (Conv2D)           (None, 14, 14, 384)       147456    
_________________________________________________________________
conv_pw_7_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_7_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_dw_8 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      
_________________________________________________________________
conv_dw_8_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_dw_8_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pw_8 (Conv2D)           (None, 14, 14, 384)       147456    
_________________________________________________________________
conv_pw_8_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_8_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_dw_9 (DepthwiseConv2D)  (None, 14, 14, 384)       3456      
_________________________________________________________________
conv_dw_9_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_dw_9_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pw_9 (Conv2D)           (None, 14, 14, 384)       147456    
_________________________________________________________________
conv_pw_9_bn (BatchNormaliza (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_9_relu (ReLU)        (None, 14, 14, 384)       0         
_________________________________________________________________
conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 384)       3456      
_________________________________________________________________
conv_dw_10_bn (BatchNormaliz (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_dw_10_relu (ReLU)       (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pw_10 (Conv2D)          (None, 14, 14, 384)       147456    
_________________________________________________________________
conv_pw_10_bn (BatchNormaliz (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_10_relu (ReLU)       (None, 14, 14, 384)       0         
_________________________________________________________________
conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 384)       3456      
_________________________________________________________________
conv_dw_11_bn (BatchNormaliz (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_dw_11_relu (ReLU)       (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pw_11 (Conv2D)          (None, 14, 14, 384)       147456    
_________________________________________________________________
conv_pw_11_bn (BatchNormaliz (None, 14, 14, 384)       1536      
_________________________________________________________________
conv_pw_11_relu (ReLU)       (None, 14, 14, 384)       0         
_________________________________________________________________
conv_pad_12 (ZeroPadding2D)  (None, 16, 16, 384)       0         
_________________________________________________________________
conv_dw_12 (DepthwiseConv2D) (None, 7, 7, 384)         3456      
_________________________________________________________________
conv_dw_12_bn (BatchNormaliz (None, 7, 7, 384)         1536      
_________________________________________________________________
conv_dw_12_relu (ReLU)       (None, 7, 7, 384)         0         
_________________________________________________________________
conv_pw_12 (Conv2D)          (None, 7, 7, 768)         294912    
_________________________________________________________________
conv_pw_12_bn (BatchNormaliz (None, 7, 7, 768)         3072      
_________________________________________________________________
conv_pw_12_relu (ReLU)       (None, 7, 7, 768)         0         
_________________________________________________________________
conv_dw_13 (DepthwiseConv2D) (None, 7, 7, 768)         6912      
_________________________________________________________________
conv_dw_13_bn (BatchNormaliz (None, 7, 7, 768)         3072      
_________________________________________________________________
conv_dw_13_relu (ReLU)       (None, 7, 7, 768)         0         
_________________________________________________________________
conv_pw_13 (Conv2D)          (None, 7, 7, 768)         589824    
_________________________________________________________________
conv_pw_13_bn (BatchNormaliz (None, 7, 7, 768)         3072      
_________________________________________________________________
conv_pw_13_relu (ReLU)       (None, 7, 7, 768)         0         
_________________________________________________________________
global_average_pooling2d_1 ( (None, 768)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 100)               76900     
_________________________________________________________________
dropout_2 (Dropout)          (None, 100)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 50)                5050      
_________________________________________________________________
dropout_3 (Dropout)          (None, 50)                0         
_________________________________________________________________
dense_5 (Dense)              (None, 4)                 204       
=================================================================
Total params: 1,915,130
Trainable params: 82,154
Non-trainable params: 1,832,976
_________________________________________________________________
Epoch 1/20
36/36 [==============================] - 1s 32ms/step - loss: 1.0827 - accuracy: 0.5235
Epoch 2/20
36/36 [==============================] - 1s 26ms/step - loss: 0.4967 - accuracy: 0.8183
Epoch 3/20
36/36 [==============================] - 1s 26ms/step - loss: 0.3198 - accuracy: 0.8713
Epoch 4/20
36/36 [==============================] - 1s 25ms/step - loss: 0.1904 - accuracy: 0.9365
Epoch 5/20
36/36 [==============================] - 1s 26ms/step - loss: 0.1455 - accuracy: 0.9513
Epoch 6/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0985 - accuracy: 0.9687
Epoch 7/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0802 - accuracy: 0.9730
Epoch 8/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0598 - accuracy: 0.9826
Epoch 9/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0445 - accuracy: 0.9852
Epoch 10/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0540 - accuracy: 0.9817
Epoch 11/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0442 - accuracy: 0.9852
Epoch 12/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0394 - accuracy: 0.9896
Epoch 13/20
36/36 [==============================] - 1s 25ms/step - loss: 0.0332 - accuracy: 0.9904
Epoch 14/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0346 - accuracy: 0.9896
Epoch 15/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0236 - accuracy: 0.9948
Epoch 16/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0221 - accuracy: 0.9957
Epoch 17/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0227 - accuracy: 0.9931
Epoch 18/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0218 - accuracy: 0.9957
Epoch 19/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0195 - accuracy: 0.9930
Epoch 20/20
36/36 [==============================] - 1s 26ms/step - loss: 0.0174 - accuracy: 0.9948
In [39]:
plt.plot(history.history['loss'])
plt.plot(history.history['accuracy'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['loss', 'accuracy'], loc='upper left')
plt.show()
In [53]:
def predict_shape(img_path):
#img_path="3D_Shapes_Dataset/cube/00000963.jpg"
    preprocessed_image = prepare_image(img_path)
    predictions_shape = finetune_model.predict(preprocessed_image) 
    labels=['Cube','Cylinder','Spheroid','Sphere']
    #print("Input Image :")
    img=cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
    plt.imshow(img)
    print("Shape Detected: ", labels[predictions_shape[0].tolist().index(max(predictions_shape[0]))])
In [54]:
img_path="3D_Shapes_Dataset/cube/00000963.jpg"
predict_shape(img_path)
Shape Detected:  Cube
In [50]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/IMG_20210626_141530.jpg -O sample1.jpg
--2021-06-26 08:47:55--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/IMG_20210626_141530.jpg
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.66.84
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.66.84|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3324161 (3.2M) [image/jpeg]
Saving to: ‘sample1.jpg’

sample1.jpg         100%[===================>]   3.17M  --.-KB/s    in 0.04s   

2021-06-26 08:47:55 (85.0 MB/s) - ‘sample1.jpg’ saved [3324161/3324161]

In [55]:
# Real-Life example
img_path="sample1.jpg"
predict_shape(img_path)
Shape Detected:  Cube
In [44]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample2.jpg -O sample2.jpg
--2021-06-26 08:39:08--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample2.jpg
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.156.63
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.156.63|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4902153 (4.7M) [image/jpeg]
Saving to: ‘sample2.jpg’

sample2.jpg         100%[===================>]   4.67M  --.-KB/s    in 0.04s   

2021-06-26 08:39:08 (118 MB/s) - ‘sample2.jpg’ saved [4902153/4902153]

In [56]:
# Real-Life example 2
img_path="sample2.jpg"
predict_shape(img_path)
Shape Detected:  Spheroid
In [46]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample3.jpg -O sample3.jpg
--2021-06-26 08:39:12--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample3.jpg
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.158.7
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.158.7|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25853 (25K) [image/jpeg]
Saving to: ‘sample3.jpg’

sample3.jpg         100%[===================>]  25.25K  --.-KB/s    in 0.001s  

2021-06-26 08:39:12 (38.8 MB/s) - ‘sample3.jpg’ saved [25853/25853]

In [57]:
# Real-Life example 3
img_path="sample3.jpg"
predict_shape(img_path)
Shape Detected:  Sphere
In [48]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample4.jpg -O sample4.jpg
--2021-06-26 08:39:15--  https://cainvas-static.s3.amazonaws.com/media/user_data/AMZzee/sample4.jpg
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.158.7
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.158.7|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12481 (12K) [image/jpeg]
Saving to: ‘sample4.jpg’

sample4.jpg         100%[===================>]  12.19K  --.-KB/s    in 0s      

2021-06-26 08:39:15 (245 MB/s) - ‘sample4.jpg’ saved [12481/12481]

In [58]:
# Real-Life example 4
img_path="sample4.jpg"
predict_shape(img_path)
Shape Detected:  Cylinder
In [ ]: