Constructing a CNN network for Dogs and Cats dataset

This data has no label.def process_test_data(): testing_data = [] for img in tqdm(os.listdir(TEST_DIR)): path = os.path.join(TEST_DIR,img) img_num = img.split('.')[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_dataNow, we can run the training:train_data = create_train_data()# If you have already created the dataset:#train_data = np.load('train_data.npy')Next, we’re ready to define our neural network:import tflearnfrom tflearn.layers.conv import conv_2d, max_pool_2dfrom tflearn.layers.core import input_data, dropout, fully_connectedfrom tflearn.layers.estimator import regressionconvnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')convnet = conv_2d(convnet, 32, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = fully_connected(convnet, 1024, activation='relu')convnet = dropout(convnet, 0.8)convnet = fully_connected(convnet, 2, activation='softmax')convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')model = tflearn.DNN(convnet, tensorboard_dir='log')What we have here is a nice, 2 layered convolutional neural network, with a fully connected layer, and then the output layer..It’s been debated whether or not a fully connected layer is of any use..I’ll leave it in anyway.This exact convnet was good enough for recognizing hand 28×28 written digits..Let’s see how it does with cats and dogs at 50×50 resolution.Now, it wont always be the case that you’re training the network fresh every time..Maybe first you just want to see how 3 epochs trains, but then, after 3, maybe you’re done, or maybe you want to see about 5 epochs..We want to be saving our model after every session, and reloading it if we have a saved version, so I will add this:if os.path.exists('{}.meta'.format(MODEL_NAME)): model.load(MODEL_NAME) print('model loaded!')Now, let’s split out training and testing data:train = train_data[:-500]test = train_data[-500:]Now, the training data and testing data are both labeled datasets..The training data is what we’ll fit the neural network with, and the test data is what we’re going to use to validate the results..The test data will be “out of sample,” meaning the testing data will only be used to test the accuracy of the network, not to train it.We also have “test” images that we downloaded..THOSE images are not labeled at all, and those are what we’ll submit to Kaggle for the competition.Next, we’re going to create our data arrays..For some reason, typical numpy logic like:array[:,0] and array[:,1] did NOT work for me here..Not sure what I’m doing wrong, so I do this instead to separate my features and labels:X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)Y = [i[1] for i in train]test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)test_y = [i[1] for i in test]Now we fit for 3 epochs:model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)Training Step: 1148 | total loss: 11.71334 | time: 4.061s| Adam | epoch: 003 | loss: 11.71334 – acc: 0.4913 — iter: 24448/24500Training Step: 1149 | total loss: 11.72928 | time: 5.074s| Adam | epoch: 003 | loss: 11.72928 – acc: 0.4906 | val_loss: 11.88134 – val_acc: 0.4840 — iter: 24500/24500–Increasing the size of the networkFirst, we need to reset the graph instance, since we’re doing this in a continuous environment:convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')convnet = conv_2d(convnet, 32, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 128, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 64, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = conv_2d(convnet, 32, 5, activation='relu')convnet = max_pool_2d(convnet, 5)convnet = fully_connected(convnet, 1024, activation='relu')convnet = dropout(convnet, 0.8)convnet = fully_connected(convnet, 2, activation='softmax')convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')model = tflearn.DNN(convnet, tensorboard_dir='log')if os.path.exists('{}.meta'.format(MODEL_NAME)): model.load(MODEL_NAME) print('model loaded!')train = train_data[:-500]test = train_data[-500:]X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)Y = [i[1] for i in train]test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)test_y = [i[1] for i in test]model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)Training Step: 4978 | total loss: 0.31290 | time: 4.031s| Adam | epoch: 010 | loss: 0.31290 – acc: 0.8641 — iter: 24448/24500Training Step: 4979 | total loss: 0.30547 | time: 5.044s| Adam | epoch: 010 | loss: 0.30547 – acc: 0.8683 | val_loss: 0.57259 – val_acc: 0.7980 — iter: 24500/24500. More details

Leave a Reply