0

I've created a CNN that classifies 120 dog breeds. I've run it for 1000 epochs and I get a validation rate of 80%. I'm trying to validate it with pictures from outside the database and it gives me an array with 120 values, all of which are 0 except one. That makes sense to me that the one that is 1 is the selection.

import cv2
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
sess = tf.compat.v1.Session(config=config)

DATADIR = "C:/Users/samue/Documents/Datasets/DogBreeds/images/Images"
CATEGORIES = os.listdir("C:/Users/samue/Documents/Datasets/DogBreeds/images/Images")

for category in CATEGORIES:
    path = os.path.join(DATADIR, category)
    class_num = CATEGORIES.index(category)

def prepare(filepath):
    IMG_SIZE = 50
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    plt.matshow(new_array)
    plt.show() 
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

model = tf.keras.models.load_model("dogbreeds.model")

prediction = model.predict([prepare('C:/validation/german shepard.jpg')])

result = np.where(prediction == 1)
res = int(''.join(map(str, result[1])))
breed = (CATEGORIES[res])
print(breed[10:])

The problem is, when I take that prediction and try and print the name for the breed, it gets the answer wrong. How can I figure out if 1. My model is garbage OR 2. The way I'm assigning the names is wrong. This is the code for the training data file and the model itself.


import numpy as np
import os
import cv2
import random as rand
import pickle
import tensorflow as tf

config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
sess = tf.compat.v1.Session(config=config)

DATADIR = "C:/Users/samue/Documents/Datasets/DogBreeds/images/Images"
CATEGORIES = os.listdir("C:/Users/samue/Documents/Datasets/DogBreeds/images/Images")

IMG_SIZE = 50

training_data = []

def create_training_data():

    for category in CATEGORIES:
        path = os.path.join(DATADIR, category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([new_array, class_num])
            except Exception as e:
                pass
create_training_data()

rand.shuffle(training_data)

X = []
y = []

for features, label in training_data:
    X.append(features)
    y.append(label)

X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
y = to_categorical(y, 120)

pickle_out = open("X.pickle", "wb")
pickle.dump(X, pickle_out)
pickle_out.close()

pickle_out = open("y.pickle", "wb")
pickle.dump(y, pickle_out)
pickle_out.close()

import numpy as np
import time
import pickle

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Activation, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator

config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
sess = tf.compat.v1.Session(config=config)

pickle_in = open("X.pickle", "rb")
X = pickle.load(pickle_in)

pickle_in = open("y.pickle", "rb")
y = pickle.load(pickle_in)

X = X / 255

NAME = f"Dogbreeds{int(time.time())}"
tensorboard = TensorBoard(log_dir="C:\\logs\\{}".format(NAME))
print(NAME)
opt = tf.keras.optimizers.Adam(
    learning_rate=0.001,
    beta_1=0.9,
    beta_2=0.999,
    epsilon=1e-7,
    amsgrad=False,
    name="Adam",
)

model = Sequential()

model.add(Conv2D(32, (6, 6), input_shape=X.shape[1:], padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Activation("relu"))

model.add(Conv2D(64, (5, 5), padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Activation("relu"))

model.add(Conv2D(128, (4, 4), padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Activation("relu"))

model.add(Conv2D(256, (3, 3), padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Activation("relu"))
model.add(Dropout(0.3))

model.add(Flatten())

model.add(Dense(1024))
model.add(Activation("relu"))

model.add(Dense(512))
model.add(Activation("relu"))
model.add(Dropout(0.3))

model.add(Dense(120))
model.add(Activation("softmax"))

model.compile(loss = "categorical_crossentropy",
    optimizer=opt,
    metrics=["accuracy"])

model.fit(
X, np.squeeze(y), batch_size=128,
epochs=1000, verbose=1,
validation_split=0.1,
callbacks=[tensorboard])
model.save("dogbreeds.model")

1
  • this is quite a lot of code to comprehend, if you could perhaps just provide us with the particular lines of code you have a doubt about that would be preferential Commented Jan 9, 2021 at 21:06

1 Answer 1

0

Use argmax:

# result = np.where(prediction == 1) # incorrect
result = np.argmax(prediction, -1)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.