Issue
Intro/setup
I am new at programming, and i made my first CNN model from a tutorial. I have set my jupyter/tensorflow/keras up in C:\Users\labadmin
What i have understood is that i just have to put the path from labadmin in order to implement my data for testing and training.
Since i am not sure what is causing the error i have pasted the whole code and error, i think it is about the system not getting the data.
The folder with the Data set-up as following:
labadmin has a folder called data withing that there are two folders training and test
Both cat images and dog images are shuffled in both folders. There are 10000 pictures in each folder, so there should be enough:
The tutorial teaches.
1. How to create a model
2. Define your labels
3. Create your training data
4. Creating and building the layers
5. Create your testing data
6. (from what i understood) the last part of the code i have created is
validating my model.
This is the code
import cv2
import numpy as np
import os
from random import shuffle
from tqdm import tqdm
TRAIN_DIR = "data\\training"
TEST_DIR = "data\\test"
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'dogvscats-{}-{}.model'.format(LR, '2cov-basic1')
def label_img(img):
word_label = img.split('.')[-3]
if word_label == 'cat': return [1,0]
elif word_label == 'dog': return [0,1]
def creat_train_data():
training_data = []
for img in tqdm(os.listdir(TRAIN_DIR)):
label = label_img(img)
path = os.path.join(TRAIN_DIR,img)
img = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (IMG_SIZE,IMG_SIZE))
training_data.append([np.array(img), np.array(label)])
shuffle(training_data)
np.save('training.npy', training_data) #save file
return training_data
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
# Building convolutional convnet
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
# http://tflearn.org/layers/conv/
# http://tflearn.org/activations/
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
#OUTPUT layer
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')
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] #ID of pic=img_num
img = cv2.resize(cv2-imread(path, cv2.IMREAD_GRAYSCALE), (IMG_SIZE,IMG_SIZE))
testing_data.append([np.array(img), img_num])
np.save('test_data.npy', testing_data)
return testing_data
train_data = creat_train_data()
#if you already have train data:
#train_data = np.load('train_data.npy')
100%|███████████████████████████████████████████████████████████████████████████| 21756/21756 [02:39<00:00, 136.07it/s]
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) #feature set
Y= [i[1] for i in test] #label
test_x = np.array([i[0] for i in train]).reshape( -1, IMG_SIZE, IMG_SIZE, 1)
test_y= [i[1] for i in test]
model.fit({'input': X}, {'targets': Y}, n_epoch=5, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
Training Step: 1664 | total loss: 9.55887 | time: 63.467s
| Adam | epoch: 005 | loss: 9.55887 - acc: 0.5849 -- iter: 21248/21256
Training Step: 1665 | total loss: 9.71830 | time: 74.722s
| Adam | epoch: 005 | loss: 9.71830 - acc: 0.5779 | val_loss: 9.81653 - val_acc: 0.5737 -- iter: 21256/21256
--
Three Questions
I have three issues which i have tried to solve, but i have had no luck finding solutions:
The first appears at: # Building convolutional convnet
curses is not supported on this machine (please install/reinstall curses for an optimal experience)
WARNING:tensorflow:From C:\Users\labadmin\Miniconda3\envs\tensorflow\lib\site-packages\tflearn\initializations.py:119: UniformUnitScaling.__init__ (from tensorflow.python.ops.init_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.initializers.variance_scaling instead with distribution=uniform to get equivalent behavior.
WARNING:tensorflow:From C:\Users\labadmin\Miniconda3\envs\tensorflow\lib\site-packages\tflearn\objectives.py:66: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
The second appears at: print('model loaded!')
if os.path.exists('{}<.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')
Where the code does not print, does that mean that the data is not loaded?
The third
Tutorial does not go through how can i test my model with a image. So how and what can i add to code that take the model (which also is being saved), and run an image from my folder with the given output being the classification?
Solution
1st: the warning messages are clear, follow it and the warning will be gone. But don't worry, you still can run your code normally if you don't.
2nd: Yes. If the model load!
is not printed out, the model is not loaded, check your path to the model file.
3rd: To save model after training, use model.save("PATH-TO-SAVE")
. Then you can load it by model.load("PATH-TO-MODEL")
.
For prediction, use model.predict({'input': X})
. See here http://tflearn.org/getting_started/#trainer-evaluator-predictor
2nd question
- To save and load a model, use
# Save a model
model.save('path-to-folder-you-want-to-save/my_model.tflearn')
# Load a model
model.load('the-folder-where-your-model-located/my_model.tflearn')
remember that you should have the extension for the model file, which is .tflearn
.
- To predict, you need to load the image just like when you load it for training.
test_image = cv2.resize(cv2.imread("path-of-the-image", cv2.IMREAD_GRAYSCALE), (IMG_SIZE,IMG_SIZE))
test_image = np.array(test_image).reshape( -1, IMG_SIZE, IMG_SIZE, 1)
prediction = model.predict({'input': test_image })
Answered By - Ha Bom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.