I've created a CNN for image recognition (flower types – 5 classes) and am now considering model parameter changes to improve accuracy. The model (5 3 * 3conv + 2 * 2max pooling layer) achieves a ~ 59% accuracy in the test set over 20 epochs, and through this Keras implementation with 1 Conv + Max pooling layer (https: // www .kaggle). com / jagadeeshkotra / flower appreciation-classification-with keras). What factors can affect the relative underperformance of my model compared to implementation at one level? Code below.

I also implemented the 1-layer Keras implementation in Vanilla Tensorflow with the same model parameters (but AdamOptimizer instead of AdaDeltaOptimizer) and achieved an accuracy of ~ 44%, so the issues are related.

Record: https://www.kaggle.com/alxmamaev/flowers-recognition

```
& # 39; & # 39; & # 39;
Multi-class classification of floral varieties with CNN
& # 39; & # 39; & # 39;
import os
import numpy as np
Import Tensorflow as tf
# image_folders_dir is the location of folders containing images of different flower types
image_folders_dir = "C: \ User squir Dropbox ML Projects Kaggle Flower Recognition \ Flowers & # 39;
IMG_SIZE = 128 # Shrink the image to this height and width
num_classes = 5
batch_size = 32
lr = 0.01 # learning rate
dropout_keep_rate = 0.95
Epochs = 20 # Number of model data shows complete data
MODEL_NAME = & # 39; flowers - {} - {}. Model & # 39; format (lr, & # 39; conv-basic & # 39;)
Print (& # 39; Load existing preprocessed data for training (L) or preprocessing data (P)? & # 39;
Decision1 = input ()
if decision1 == & # 39; P & # 39 ;:
from preprocessing import create_data
train_data, test_data = create_data (image_folders_dir, IMG_SIZE)
elif decision1 == & # 39; L & # 39 ;:
If os.path.exists (& # 39; train_data.npy & # 39;) exists:
train_data = np.load (& # 39; train_data.npy & # 39;
test_data = np.load (& # 39; test_data.npy & # 39;)
otherwise:
Throw exception (& # 39; There is no preprocessed data in the path, please preprocess some. & # 39;
otherwise:
Raise an exception ("Please try again and enter L or P")
#data consists of a list with: [0]: Image data, [1]: Class label
# image data = IMG_SIZE * IMG_SIZE * 3 array
# either rgb or bgr? I do not know if it's important
, ''
4321 (3889train, 432test) pictures are now:
IMG_SIZE * IMG_SIZE RGB attached to a hot-class label flower type that is sorted at random
, ''
# Derive image and label data from new data records
train_data_imgs =[Items[item[Artikel[item[0] for item in train_data]train_data_lbls =[Items[item[Artikel[item[1] for item in train_data]test_data_imgs =[Items[item[Artikel[item[0] for articles in test data]test_data_lbls =[Items[item[Artikel[item[1] for articles in test data]# Create arrays in models for us
X_train = np.array (train_data_imgs) .reshape (-1, IMG_SIZE, IMG_SIZE, 3)
Y_train = train_data_lbls
x_valid = np.array (test_data_imgs) .reshape (-1, IMG_SIZE, IMG_SIZE, 3)
y_valid = test_data_lbls
# GAN Image Augmentation here
# need to normalize?
& # 39; & # 39; & # 39; model execution (training or loading) & # 39; & # 39; & # 39;
x = tf.placeholder (& # 39; float & # 39;) [None, IMG_SIZE, IMG_SIZE, 3])
y = tf.placeholder (& # 39; float & # 39;)
def conv2d (x, W):
Return tf.nn.conv2d (x, W, Strides =[1, 1, 1, 1], padding = & # 39; SAME & # 39;
def maxpool2D (x):
# 2 * 2 pool (ksize), step 2 so not overlapping
Return tf.nn.max_pool (x, ksize =[1, 2, 2, 1]strides =[1, 2, 2, 1], padding = & # 39; SAME & # 39;
def conv_NN_model (x, num_classes, img_size, keep_rate):
# 5 x 5 kernel, 3 input depth (RGB image), 32 output depth (convolution)
Weights = {& # 39; W_conv1 & # 39 ;: tf.Variable (tf.random_normal ([3, 3, 3, 32])),
& # 39; W_conv2 & # 39 ;: tf.Variable (tf.random_normal ([3, 3, 32, 64])),
& # 39; W_conv3 & # 39 ;: tf.Variable (tf.random_normal ([3, 3, 64, 128])),
& # 39; W_conv4 & # 39 ;: tf.Variable (tf.random_normal ([3, 3, 128, 256])),
& # 39; W_conv5 & # 39 ;: tf.Variable (tf.random_normal ([3, 3, 256, 512])),
& # 39; W_fc & # 39 ;: tf.Variable (tf.random_normal ([4 * 4 * 512, 1024])), # conv output layer size, number of neurons in fc
& # 39; out & # 39 ;: tf.Variable (tf.random_normal ([1024, num_classes]))}
vases = {& bgr; b_conv1 & # 39 ;: tf.Variable (tf.random_normal ([32])),
& Bgr; b_conv2 & # 39 ;: tf.Variable (tf.random_normal ([64])),
& Bgr; b_conv3 & # 39 ;: tf.Variable (tf.random_normal ([128])),
& Bgr; b_conv4 & # 39 ;: tf.Variable (tf.random_normal ([256])),
& Bgr; b_conv5 & # 39 ;: tf.Variable (tf.random_normal ([512])),
& Bgr; b_fc & # 39 ;: tf.Variable (tf.random_normal ([1024])), # conv output layer size, number of neurons in fc
& # 39; out & # 39 ;: tf.Variable (tf.random_normal ([num_classes]))}
x = tf.reshape (x, shape =[-1, img_size, img_size, 3])
# tf.reshape (x, shape =[28,28]) have the same effect?
conv1 = conv2d (x, weights['W_conv1'])
conv1 = maxpool2D (conv1)
conv2 = conv2d (conv1, weights['W_conv2'])
conv2 = maxpool2D (conv2)
conv3 = conv2d (conv2, weights['W_conv3'])
conv3 = maxpool2D (conv3)
conv4 = conv2d (conv3, weights['W_conv4'])
conv4 = maxpool2D (conv4)
conv5 = conv2d (conv4, weights['W_conv5'])
conv5 = maxpool2D (conv5)
fc = tf.reshape (conv5, [-1, 4 * 4 * 512])
fc = tf.matmul (fc, weights['W_fc']) + Prejudices['b_fc']
fc = tf.nn.relu (fc)
fc = tf.nn.dropout (fc, keep_prob = keep_rate)
output = tf.matmul (fc, weights['out']) + Prejudices['out']
Return the output
def next_batch (num, data, labels):
& # 39; & # 39; & # 39;
Return a total of "num" samples and labels.
& # 39; & # 39; & # 39;
idx = np.arange (0, len (data))
np.random.shuffle (idx)
idx = idx[:num]
data_shuffle = [data[i] for i in idx]labels_shuffle = [labels[i] for i in idx]Return np.asarray (data_shuffle), np.asarray (labels_shuffle)
def train_neural_network (x, num_epochs, train_data, batch_size, train_imgs, train_lbls, test_imgs, test_lbls):
Prediction = conv_NN_model (x = x,
num_classes = num_classes,
img_size = IMG_SIZE,
keep_rate = dropout_keep_rate)
cost = tf.reduce_mean (tf.nn.softmax_cross_entropy_with_logits_v2 (logits = prediction, labels = y))
Optimizer = tf.train.AdamOptimizer (learning_rate = 0.001) .minimize (cost)
#optimizer = tf.train.GradientDescentOptimizer (learning_rate = 0,001) .minimize (cost)
with tf.Session () as session:
sess.run (tf.global_variables_initializer ())
for epoch within reach (num_epochs):
epoch_loss = 0
for _ in the range (int (len (train_data) / batch_size)):
epoch_x, epoch_y = next_batch (batch_size, train_imgs, train_lbls)
_, c = sess.run ([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})
epoch_loss + = c
print (& # 39; epoch & #;; epoch & # 39; completed & # 39 ;, & # 39; num_epochs & # 39 ;, loss: & # 39; epoch_loss)
right = tf.equal (tf.argmax (prediction, 1), tf.argmax (y, 1))
Accuracy = tf.reduce_mean (tf.cast (correct, & # 39; float & # 39;))
print (& # 39; accuracy: & # 39 ;, precision.eval ({x: test_imgs, y: test_lbls}))
train_neural_network (x = x,
num_epochs = epochs,
train_data = train_data,
batch_size = batch_size,
train_imgs = train_data_imgs,
train_lbls = train_data_lbls,
test_imgs = test_data_imgs,
test_lbls = test_data_lbls)
& # 39; & # 39; & # 39; preparation module
Data is displayed in 5 different folders, one for each class ~ 800 images per folderA
task = Create two .npy files with image data and labels with approximately equal distribution of flower types
Train = 90%, test = 10%
combine in a single list with 2 arrays. [0] = Image data, [1] = a hot coded class name
, ''
import os
import cv2
import numpy as np
from tqdm import tqdm
from the random import mix
def create_data (folder_dir, img_size):
data = [] # create complete data with (numeric) labels
for fold in os.listdir (folder_dir):
# a hot coding class label
if fold == & # 39; daisy & # 39;: label = [1, 0, 0, 0, 0]
Eliffalte == & # 39; dandelion & # 39 ;: label = [0, 1, 0, 0, 0]
elif fold == & rose; #: label = [0, 0, 1, 0, 0]
elif fold == & # 39; sunflower & # 39 ;: label = [0, 0, 0, 1, 0]
elif fold == & # 39; tulip & # 39 ;: label = [0, 0, 0, 0, 1]
for img in tqdm (os.listdir (os.path.join (folder_dir, str (fold)))):
path = os.path.join (folder_dir, fold, img)
img = cv2.resize (cv2.imread (path, 1), (img_size, img_size))
data.append ([np.array(img), label])
Shuffle (data)
training_data = data[:4105]
testing_data = data[4105:]
np.save (& # 39; train_data.npy & # 39; training_data)
np.save (& # 39; test_data.npy & # 39; testing_data)
Return training_data, testing_data
```