<jupyter_start><jupyter_text>Model<jupyter_code>input_image = Input(shape=(28,28,3), batch_shape=(None,28,28,3), dtype=float,name='Input_Image') 
conv = Conv2D(filters=10, kernel_size=5, padding='same', activation='relu')(input_image)
conv = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(conv) 
conv = Conv2D(filters=8, kernel_size=3, padding='valid', activation='relu')(conv)
conv = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(conv)
fmap = Flatten()(conv)
# Classification Head 1 - Length Classification
x = Dense(128, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(fmap)
x = Dense(32, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
length = Dense(1, activation='sigmoid', name='length', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
# Classification Head 2 - Width Classification
x = Dense(64, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(fmap)
x = Dense(8, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
width = Dense(1, activation='sigmoid', name='width', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
# Classification Head 3 - Colour Classification
x = Dense(64, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(fmap)
x = Dense(8, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
colour = Dense(1, activation='sigmoid', name='colour', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
# Classification Head 4 - Angle Classification
x = Dense(128, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(fmap)
x = Dense(64, activation='relu', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
angle = Dense(12, activation='softmax', name='angle', kernel_initializer=RandomNormal(stddev=0.01), bias_initializer=RandomNormal(stddev=0.01))(x)
model = Model(inputs=input_image, 
              outputs=[length, width, colour, angle])
model.compile(optimizer='adam',
              loss={'length':'binary_crossentropy',
                    'width':'binary_crossentropy',
                    'colour':'binary_crossentropy',
                    'angle':'categorical_crossentropy'},
              metrics={'length':'accuracy',
                    'width':'accuracy',
                    'colour':'accuracy',
                    'angle':'accuracy'},
              loss_weights=[5,2,1,50])
print(model.summary())
history1 = model.fit(x=x_train,
                  y={'length':y_length_train,
                     'width':y_width_train,
                     'colour':y_colour_train,
                     'angle':y_angle_train},
                  batch_size=3200,
                  epochs=15,
                  verbose=1,
                  validation_data=(x_validate, 
                    {'length':y_length_validate,
                     'width':y_width_validate,
                     'colour':y_colour_validate,
                     'angle':y_angle_validate}),
                  shuffle=False # data is pre-shuffled, don't waste computation time
                 )
model.save('Q2-v3.h5')
metrics = model.evaluate(x=x_test,
                       y={'length':y_length_test,
                          'width':y_width_test,
                          'colour':y_colour_test,
                          'angle':y_angle_test},
                       verbose=1
                      )
print(model.metrics_names, metrics)
print(history1.history.keys())
plt.plot(history1.history['loss'], label='total loss')
plt.plot(history1.history['length_loss'], label='length loss')
plt.plot(history1.history['width_loss'], label='width loss')
plt.plot(history1.history['colour_loss'], label='colour loss')
plt.plot(history1.history['angle_loss'], label='angle loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(np.array(history1.history['loss'])/58, label='Normalized total loss')
plt.plot(history1.history['length_loss'], label='length loss')
plt.plot(history1.history['width_loss'], label='width loss')
plt.plot(history1.history['colour_loss'], label='colour loss')
plt.plot(history1.history['angle_loss'], label='angle loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(history1.history['length_acc'], label='Tr: Length')
plt.plot(history1.history['width_acc'], label='Tr: Width')
plt.plot(history1.history['colour_acc'], label='Tr: Colour')
plt.plot(history1.history['angle_acc'], label='Tr: Angle')
plt.xlabel('Epoch')
plt.ylabel('Training Accuracy')
plt.legend()
plt.show()
plt.plot(history1.history['val_length_acc'], label='Val: Length')
plt.plot(history1.history['val_width_acc'], label='Val: Width')
plt.plot(history1.history['val_colour_acc'], label='Val: Colour')
plt.plot(history1.history['val_angle_acc'], label='Val: Angle')
plt.xlabel('Epoch')
plt.ylabel('Validation Accuracy')
plt.legend()
plt.show()
length_p, width_p, colour_p, angle_p = model.predict(x=x_test, verbose=1)
# Convert_to_class_labels
length_p = length_p.round().flatten()
width_p = width_p.round().flatten()
colour_p = colour_p.round().flatten()
angle_p = angle_p.argmax(axis=1)
# Get absolute class label
def get_absolute_class_label(l,w,c,a):
    return 42*l + 24*w + 12*c + a
y = get_absolute_class_label(y_length_test, y_width_test, y_colour_test, y_angle_test.argmax(axis=1))
y_hat = get_absolute_class_label(length_p, width_p, colour_p, angle_p)
print('Accuracies:')
acc_length = accuracy_score(y_length_test, length_p.round())
print('Length:', acc_length)
acc_width = accuracy_score(y_width_test, width_p.round())
print('Width:', acc_width)
acc_colour = accuracy_score(y_colour_test, colour_p.round())
print('Colour:', acc_colour)
acc_angle = accuracy_score(y_angle_test.argmax(axis=1), angle_p)
print('Angle:', acc_angle)
print('Average Accuracy:', np.average([acc_length, acc_width, acc_colour, acc_angle]))
conmat = confusion_matrix(y, y_hat)
np.savez_compressed('v3_conmat.npz', conmat=conmat)
fig, ax = plt.subplots(1,1,figsize=(25,22))
im = ax.imshow(conmat)
plt.colorbar(im)
plt.show()
recall = recall_score(y, y_hat, average=None)
precision = precision_score(y, y_hat, average=None)
fscores = f1_score(y, y_hat, average=None)
np.savez_compressed('v3fscores.npz', fscores=fscores, recall=recall, precision=precision)
plt.plot(recall, label='Precision')
plt.plot(recall, label='Recall')
plt.plot(fscores, label='F Score')
plt.xlabel('Class')
plt.ylabel('Score')
plt.legend()
plt.show()
print('------Length-----')
length_conmat = confusion_matrix(y_length_test, length_p)
print(length_conmat)
length_fscore = f1_score(y_length_test, length_p)
length_precision = precision_score(y_length_test, length_p)
length_recall = recall_score(y_length_test, length_p)
print('Precision:', length_precision)
print('Recall:', length_recall)
print('F score:', length_fscore)
print('------Width-----')
width_conmat = confusion_matrix(y_width_test, width_p)
print(width_conmat)
width_fscore = f1_score(y_width_test, width_p)
width_precision = precision_score(y_width_test, width_p)
width_recall = recall_score(y_width_test, width_p)
print('Precision:', width_precision)
print('Recall:', width_recall)
print('F score:', width_fscore)
print('------Colour-----')
colour_conmat = confusion_matrix(y_colour_test, colour_p)
print(colour_conmat)
colour_fscore = f1_score(y_colour_test, colour_p)
colour_precision = precision_score(y_colour_test, colour_p)
colour_recall = recall_score(y_colour_test, colour_p)
print('Precision:', colour_precision)
print('Recall:', colour_recall)
print('F score:', colour_fscore)
print('------Angle-----')
angle_conmat = confusion_matrix(y_angle_test.argmax(axis=1), angle_p)
print(angle_conmat)
angle_fscore = f1_score(y_angle_test.argmax(axis=1), angle_p, average=None)
angle_precision = precision_score(y_angle_test.argmax(axis=1), angle_p, average=None)
angle_recall = recall_score(y_angle_test.argmax(axis=1), angle_p, average=None)
plt.plot(angle_precision, label='Precision')
plt.plot(angle_recall, label='Recall')
plt.plot(angle_fscore, label='F Score')
plt.xlabel('Angles')
plt.ylabel('Score')
plt.legend()
plt.show()
np.savetxt('angle_conmat_v3.txt', angle_conmat, delimiter=' & ', fmt='%3d', newline='\\\\\n')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15818750724301772, 'fraction_numerical': 0.02213466218565303, 'mean_word_length': 5.710730948678072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '123536', 'license': 'MIT', 'n_tokens_mistral': 3160, 'n_tokens_neox': 2934, 'n_words': 429, 'path': 'Assignments/Assignment_2/Q2/Q2-v3.ipynb', 'repo_name': 'Kaustubh1Verma/CS671_Deep-Learning_2019', 'size': '8629'}