<jupyter_start><jupyter_text>Jupyter notebook for quickly testing accuracy/F1-score imports necessary packages<jupyter_code>import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from random import shuffle 
from tqdm import tqdm
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

import tensorflow as tf<jupyter_output>WARNING:tensorflow:From /home/pallab/cvp/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use the retry module or similar alternatives.
hdf5 is not supported on this machine (please install/reinstall h5py for optimal experience)<jupyter_text>Define constants and directories<jupyter_code>LR = 1e-3
IMG_DIR = '/home/pallab/gestures-cnn/images/qtest/'
IMG_SIZE = 100
MODEL_DIR = '/home/pallab/gestures-cnn/tfmodels/'
MODEL_NAME = 'gesture-{}-{}.model'.format(LR, 'commit') 
def process_test_data():
    testing_data = []
    for img in tqdm(os.listdir(IMG_DIR)):
        path = os.path.join(IMG_DIR,img)
        img_num = img[0]
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        testing_data.append([np.array(img), img_num])
        
    shuffle(testing_data)
    np.save('test_data.npy', testing_data)
    return testing_data
tf.reset_default_graph()
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

# 7 Layered CNN Architecture with 5 Convolutional and 2 FC Layer with Dropout and Max Pooling

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = dropout(convnet, 0.8)
print(convnet.shape)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = dropout(convnet, 0.8)
print(convnet.shape)

convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = dropout(convnet, 0.8)
print(convnet.shape)

convnet = conv_2d(convnet, 256, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = dropout(convnet, 0.8)
print(convnet.shape)

convnet = conv_2d(convnet, 512, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = dropout(convnet, 0.8)
print(convnet.shape)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 4, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.load(MODEL_DIR + MODEL_NAME)
try:
    test_data = np.load(IMG_DIR + 'test_data.npy')
except (FileNotFoundError):
    test_data = process_test_data()
fig = plt.figure()
mark = {"i": 1, "v": 2, "f": 3, "t": 4}
labelss = []
predics = []
row, col = 1, 1
str_label = ""
print("len =", len(test_data))
# for num, data in enumerate(test_data):
#     img_num = data[1]
#     img_data = data[0]
#     labelss.append(mark[img_num])
#     y = fig.add_subplot(row, col, num+1)
#     orig = img_data
#     data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)
#     model_out = (model.predict([data])[0]).round()
#     if np.array_equal((model_out),np.array([1.,0.,0.,0.])):
#         str_label = 'index'
#         predics.append(1)
#     elif np.array_equal((model_out),np.array([0.,1.,0.,0.]) ): 
#         str_label = 'vshape'
#         predics.append(2)
#     elif np.array_equal((model_out) , np.array([0.,0.,1.,0.])): 
#         str_label = 'fist'
#         predics.append(3)
#     elif np.array_equal((model_out) , np.array([0.,0.,0.,1.])): 
#         str_label = 'thumb'
#         predics.append(4)
    
#     y.imshow(orig,cmap='gray')
#     plt.title(str_label)
#     y.axes.get_xaxis().set_visible(False)
#     y.axes.get_yaxis().set_visible(False)
num, data = test_data[0]
label = data
print("label =", label)
rep = num.reshape(IMG_SIZE,IMG_SIZE,1)
model_out = model.predict([rep])[0]

print(model_out)
plt.tight_layout()
plt.show()
from sklearn.metrics import classification_report
print(classification_report(labelss, predics))
print(labelss, predics)
print(str_label)<jupyter_output>[2, 2, 2, 2, 2] [1, 1, 1, 1, 1]
index
{'fraction_non_alphanumeric': 0.1422680412371134, 'fraction_numerical': 0.027033218785796106, 'mean_word_length': 4.677503250975293, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '344128', 'license': 'MIT', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1633, 'n_words': 335, 'path': 'notebooks/image_testing.ipynb', 'repo_name': 'pallabganguly/gestures-cnn', 'size': '4365'}