Can't save and load a trained CNN model for binary image classification
I have built a Binary Image classifier using Convolutional Neural Networks using TensorFlow.It is running fine, however, each time it takes too long to train from scratch. So, I want to save the trained model and load it next time. I can't seem to understand how to implement these guides in my program as shown in the TensorFlow documentation.
Here's the full code:
# Python program to create
# Image Classifier using CNN
# Importing the required libraries
import cv2
import os
import numpy as np
from random import shuffle
from tqdm import tqdm
from keras.models import Sequential
'''Setting up the env'''
TRAIN_DIR = 'D:\Project\Final_Project\chest_xray\train\'
TEST_DIR = 'D:\Project\Final_Project\chest_xray\test0\'
check_point = 'D:\Project\Final_Project\chest_xray\chkpt\'
IMG_SIZE = 80
LR = 1e-4
'''Setting up the model which will help with tensorflow models'''
MODEL_NAME = 'NormalVsAbnormalXRays-{}-{}.model'.format(LR, '6conv-basic')
'''Labelling the dataset'''
def label_img(img):
word_label = img.split('.')[-3]
# DIY One hot encoder
if word_label == 'Nor':
return [1, 0]
elif word_label == 'Pne':
return [0, 1]
else :
return[0, 0]
'''Creating the training data'''
def create_train_data():
# Creating an empty list where we should the store the training data
# after a little preprocessing of the data
training_data =
# tqdm is only used for interactive loading
# loading the training data
for img in tqdm(os.listdir(TRAIN_DIR)):
# labeling the images
label = label_img(img)
path = os.path.join(TRAIN_DIR, img)
# loading the image from the path and then converting them into
# greyscale for easier covnet prob
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# resizing the image for processing them in the covnet
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# final step-forming the training data list with numpy array of the images
training_data.append([np.array(img), np.array(label)])
# shuffling of the training data to preserve the random state of our data
shuffle(training_data)
# saving our trained data for further uses if required
np.save('train_data.npy', training_data)
return training_data
'''Processing the given test data'''
# Almost same as processing the traning data but
# we dont have to label it.
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_data
'''Running the training and the testing in the dataset for our model'''
#train_data = create_train_data()
#test_data = process_test_data()
train_data = np.load('train_data.npy')
test_data = np.load('test_data.npy')
'''Creating the neural network using tensorflow'''
# Importing the required libraries
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
model = Sequential()
tf.reset_default_graph()
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
convnet = input_data(shape=[None,IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.3)
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', checkpoint_path='check_point',best_checkpoint_path= 'check_point',max_checkpoints= 5)
# Splitting the testing data and training data
train = train_data
test = train_data
'''Setting up the features and lables'''
# X-Features & Y-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]
'''Fitting the data into our model'''
# epoch = 40 taken
model.fit({'input': X}, {'targets': Y}, n_epoch=1,
validation_set=0.05,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
'''Testing the data'''
import matplotlib.pyplot as plt
# if you need to create the data:
# test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')
fig = plt.figure(figsize=(80,80))
for num, data in enumerate(test_data[:1]):
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(1, 1, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
# model_out = model.predict([data])[0]
model_out = model.predict([data])[0]
if np.argmax(model_out) == 1:
str_label = 'Abnormal'
else:
str_label = 'Normal'
y.imshow(orig, cmap='gray')
plt.title(str_label,fontsize=20)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()
I have tried to use saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
to import the graph but I get this error
Traceback (most recent call last):
File "D:/Project/Final_Project/chest_xray/Final_CNN.py", line 104, in <module>
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1674, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1696, in _import_meta_graph_with_return_elements
**kwargs))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkmeta_graph.py", line 852, in import_scoped_meta_graph_with_return_elements
ops.prepend_name_scope(value, scope_to_prepend_to_names))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3550, in _as_graph_element_locked
"graph." % repr(name))
KeyError: "The name 'Adam' refers to an Operation not in the graph."
Process finished with exit code 1
python tensorflow conv-neural-network tflearn
add a comment |
I have built a Binary Image classifier using Convolutional Neural Networks using TensorFlow.It is running fine, however, each time it takes too long to train from scratch. So, I want to save the trained model and load it next time. I can't seem to understand how to implement these guides in my program as shown in the TensorFlow documentation.
Here's the full code:
# Python program to create
# Image Classifier using CNN
# Importing the required libraries
import cv2
import os
import numpy as np
from random import shuffle
from tqdm import tqdm
from keras.models import Sequential
'''Setting up the env'''
TRAIN_DIR = 'D:\Project\Final_Project\chest_xray\train\'
TEST_DIR = 'D:\Project\Final_Project\chest_xray\test0\'
check_point = 'D:\Project\Final_Project\chest_xray\chkpt\'
IMG_SIZE = 80
LR = 1e-4
'''Setting up the model which will help with tensorflow models'''
MODEL_NAME = 'NormalVsAbnormalXRays-{}-{}.model'.format(LR, '6conv-basic')
'''Labelling the dataset'''
def label_img(img):
word_label = img.split('.')[-3]
# DIY One hot encoder
if word_label == 'Nor':
return [1, 0]
elif word_label == 'Pne':
return [0, 1]
else :
return[0, 0]
'''Creating the training data'''
def create_train_data():
# Creating an empty list where we should the store the training data
# after a little preprocessing of the data
training_data =
# tqdm is only used for interactive loading
# loading the training data
for img in tqdm(os.listdir(TRAIN_DIR)):
# labeling the images
label = label_img(img)
path = os.path.join(TRAIN_DIR, img)
# loading the image from the path and then converting them into
# greyscale for easier covnet prob
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# resizing the image for processing them in the covnet
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# final step-forming the training data list with numpy array of the images
training_data.append([np.array(img), np.array(label)])
# shuffling of the training data to preserve the random state of our data
shuffle(training_data)
# saving our trained data for further uses if required
np.save('train_data.npy', training_data)
return training_data
'''Processing the given test data'''
# Almost same as processing the traning data but
# we dont have to label it.
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_data
'''Running the training and the testing in the dataset for our model'''
#train_data = create_train_data()
#test_data = process_test_data()
train_data = np.load('train_data.npy')
test_data = np.load('test_data.npy')
'''Creating the neural network using tensorflow'''
# Importing the required libraries
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
model = Sequential()
tf.reset_default_graph()
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
convnet = input_data(shape=[None,IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.3)
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', checkpoint_path='check_point',best_checkpoint_path= 'check_point',max_checkpoints= 5)
# Splitting the testing data and training data
train = train_data
test = train_data
'''Setting up the features and lables'''
# X-Features & Y-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]
'''Fitting the data into our model'''
# epoch = 40 taken
model.fit({'input': X}, {'targets': Y}, n_epoch=1,
validation_set=0.05,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
'''Testing the data'''
import matplotlib.pyplot as plt
# if you need to create the data:
# test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')
fig = plt.figure(figsize=(80,80))
for num, data in enumerate(test_data[:1]):
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(1, 1, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
# model_out = model.predict([data])[0]
model_out = model.predict([data])[0]
if np.argmax(model_out) == 1:
str_label = 'Abnormal'
else:
str_label = 'Normal'
y.imshow(orig, cmap='gray')
plt.title(str_label,fontsize=20)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()
I have tried to use saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
to import the graph but I get this error
Traceback (most recent call last):
File "D:/Project/Final_Project/chest_xray/Final_CNN.py", line 104, in <module>
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1674, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1696, in _import_meta_graph_with_return_elements
**kwargs))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkmeta_graph.py", line 852, in import_scoped_meta_graph_with_return_elements
ops.prepend_name_scope(value, scope_to_prepend_to_names))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3550, in _as_graph_element_locked
"graph." % repr(name))
KeyError: "The name 'Adam' refers to an Operation not in the graph."
Process finished with exit code 1
python tensorflow conv-neural-network tflearn
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
1
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57
add a comment |
I have built a Binary Image classifier using Convolutional Neural Networks using TensorFlow.It is running fine, however, each time it takes too long to train from scratch. So, I want to save the trained model and load it next time. I can't seem to understand how to implement these guides in my program as shown in the TensorFlow documentation.
Here's the full code:
# Python program to create
# Image Classifier using CNN
# Importing the required libraries
import cv2
import os
import numpy as np
from random import shuffle
from tqdm import tqdm
from keras.models import Sequential
'''Setting up the env'''
TRAIN_DIR = 'D:\Project\Final_Project\chest_xray\train\'
TEST_DIR = 'D:\Project\Final_Project\chest_xray\test0\'
check_point = 'D:\Project\Final_Project\chest_xray\chkpt\'
IMG_SIZE = 80
LR = 1e-4
'''Setting up the model which will help with tensorflow models'''
MODEL_NAME = 'NormalVsAbnormalXRays-{}-{}.model'.format(LR, '6conv-basic')
'''Labelling the dataset'''
def label_img(img):
word_label = img.split('.')[-3]
# DIY One hot encoder
if word_label == 'Nor':
return [1, 0]
elif word_label == 'Pne':
return [0, 1]
else :
return[0, 0]
'''Creating the training data'''
def create_train_data():
# Creating an empty list where we should the store the training data
# after a little preprocessing of the data
training_data =
# tqdm is only used for interactive loading
# loading the training data
for img in tqdm(os.listdir(TRAIN_DIR)):
# labeling the images
label = label_img(img)
path = os.path.join(TRAIN_DIR, img)
# loading the image from the path and then converting them into
# greyscale for easier covnet prob
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# resizing the image for processing them in the covnet
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# final step-forming the training data list with numpy array of the images
training_data.append([np.array(img), np.array(label)])
# shuffling of the training data to preserve the random state of our data
shuffle(training_data)
# saving our trained data for further uses if required
np.save('train_data.npy', training_data)
return training_data
'''Processing the given test data'''
# Almost same as processing the traning data but
# we dont have to label it.
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_data
'''Running the training and the testing in the dataset for our model'''
#train_data = create_train_data()
#test_data = process_test_data()
train_data = np.load('train_data.npy')
test_data = np.load('test_data.npy')
'''Creating the neural network using tensorflow'''
# Importing the required libraries
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
model = Sequential()
tf.reset_default_graph()
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
convnet = input_data(shape=[None,IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.3)
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', checkpoint_path='check_point',best_checkpoint_path= 'check_point',max_checkpoints= 5)
# Splitting the testing data and training data
train = train_data
test = train_data
'''Setting up the features and lables'''
# X-Features & Y-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]
'''Fitting the data into our model'''
# epoch = 40 taken
model.fit({'input': X}, {'targets': Y}, n_epoch=1,
validation_set=0.05,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
'''Testing the data'''
import matplotlib.pyplot as plt
# if you need to create the data:
# test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')
fig = plt.figure(figsize=(80,80))
for num, data in enumerate(test_data[:1]):
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(1, 1, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
# model_out = model.predict([data])[0]
model_out = model.predict([data])[0]
if np.argmax(model_out) == 1:
str_label = 'Abnormal'
else:
str_label = 'Normal'
y.imshow(orig, cmap='gray')
plt.title(str_label,fontsize=20)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()
I have tried to use saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
to import the graph but I get this error
Traceback (most recent call last):
File "D:/Project/Final_Project/chest_xray/Final_CNN.py", line 104, in <module>
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1674, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1696, in _import_meta_graph_with_return_elements
**kwargs))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkmeta_graph.py", line 852, in import_scoped_meta_graph_with_return_elements
ops.prepend_name_scope(value, scope_to_prepend_to_names))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3550, in _as_graph_element_locked
"graph." % repr(name))
KeyError: "The name 'Adam' refers to an Operation not in the graph."
Process finished with exit code 1
python tensorflow conv-neural-network tflearn
I have built a Binary Image classifier using Convolutional Neural Networks using TensorFlow.It is running fine, however, each time it takes too long to train from scratch. So, I want to save the trained model and load it next time. I can't seem to understand how to implement these guides in my program as shown in the TensorFlow documentation.
Here's the full code:
# Python program to create
# Image Classifier using CNN
# Importing the required libraries
import cv2
import os
import numpy as np
from random import shuffle
from tqdm import tqdm
from keras.models import Sequential
'''Setting up the env'''
TRAIN_DIR = 'D:\Project\Final_Project\chest_xray\train\'
TEST_DIR = 'D:\Project\Final_Project\chest_xray\test0\'
check_point = 'D:\Project\Final_Project\chest_xray\chkpt\'
IMG_SIZE = 80
LR = 1e-4
'''Setting up the model which will help with tensorflow models'''
MODEL_NAME = 'NormalVsAbnormalXRays-{}-{}.model'.format(LR, '6conv-basic')
'''Labelling the dataset'''
def label_img(img):
word_label = img.split('.')[-3]
# DIY One hot encoder
if word_label == 'Nor':
return [1, 0]
elif word_label == 'Pne':
return [0, 1]
else :
return[0, 0]
'''Creating the training data'''
def create_train_data():
# Creating an empty list where we should the store the training data
# after a little preprocessing of the data
training_data =
# tqdm is only used for interactive loading
# loading the training data
for img in tqdm(os.listdir(TRAIN_DIR)):
# labeling the images
label = label_img(img)
path = os.path.join(TRAIN_DIR, img)
# loading the image from the path and then converting them into
# greyscale for easier covnet prob
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# resizing the image for processing them in the covnet
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# final step-forming the training data list with numpy array of the images
training_data.append([np.array(img), np.array(label)])
# shuffling of the training data to preserve the random state of our data
shuffle(training_data)
# saving our trained data for further uses if required
np.save('train_data.npy', training_data)
return training_data
'''Processing the given test data'''
# Almost same as processing the traning data but
# we dont have to label it.
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_data
'''Running the training and the testing in the dataset for our model'''
#train_data = create_train_data()
#test_data = process_test_data()
train_data = np.load('train_data.npy')
test_data = np.load('test_data.npy')
'''Creating the neural network using tensorflow'''
# Importing the required libraries
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
model = Sequential()
tf.reset_default_graph()
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
convnet = input_data(shape=[None,IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 128, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 32, 4, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.3)
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', checkpoint_path='check_point',best_checkpoint_path= 'check_point',max_checkpoints= 5)
# Splitting the testing data and training data
train = train_data
test = train_data
'''Setting up the features and lables'''
# X-Features & Y-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]
'''Fitting the data into our model'''
# epoch = 40 taken
model.fit({'input': X}, {'targets': Y}, n_epoch=1,
validation_set=0.05,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
'''Testing the data'''
import matplotlib.pyplot as plt
# if you need to create the data:
# test_data = process_test_data()
# if you already have some saved:
test_data = np.load('test_data.npy')
fig = plt.figure(figsize=(80,80))
for num, data in enumerate(test_data[:1]):
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(1, 1, num + 1)
orig = img_data
data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
# model_out = model.predict([data])[0]
model_out = model.predict([data])[0]
if np.argmax(model_out) == 1:
str_label = 'Abnormal'
else:
str_label = 'Normal'
y.imshow(orig, cmap='gray')
plt.title(str_label,fontsize=20)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()
I have tried to use saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
to import the graph but I get this error
Traceback (most recent call last):
File "D:/Project/Final_Project/chest_xray/Final_CNN.py", line 104, in <module>
saver = tf.train.import_meta_graph('D:\Project\Final_Project\chest_xray\check_point-78.meta')
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1674, in import_meta_graph
meta_graph_or_file, clear_devices, import_scope, **kwargs)[0]
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythontrainingsaver.py", line 1696, in _import_meta_graph_with_return_elements
**kwargs))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkmeta_graph.py", line 852, in import_scoped_meta_graph_with_return_elements
ops.prepend_name_scope(value, scope_to_prepend_to_names))
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:UserswaqarAppDataLocalProgramsPythonPython36libsite-packagestensorflowpythonframeworkops.py", line 3550, in _as_graph_element_locked
"graph." % repr(name))
KeyError: "The name 'Adam' refers to an Operation not in the graph."
Process finished with exit code 1
python tensorflow conv-neural-network tflearn
python tensorflow conv-neural-network tflearn
asked Nov 25 '18 at 10:14
Waqar DanishWaqar Danish
4318
4318
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
1
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57
add a comment |
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
1
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
1
1
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466500%2fcant-save-and-load-a-trained-cnn-model-for-binary-image-classification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466500%2fcant-save-and-load-a-trained-cnn-model-for-binary-image-classification%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What version of TF was used to save the graph and which version re you using?
– Matthieu Brucher
Nov 25 '18 at 10:25
@MatthieuBrucher I'm using TensorFlow 1.12 and I've trained the model myself so the graph is saved in TF 1.12 and I want to load it in same version.
– Waqar Danish
Nov 25 '18 at 12:53
1
It seems that something went wrong perhaps in the save. Can you show this part as well?
– Matthieu Brucher
Nov 25 '18 at 12:56
@MatthieuBrucher I fixed it now. I saved the model using model.save(model.tfl) and loaded the same model using model.load(model.tfl).I wasn't excluding the model.fit which trained the model again from scratch. Thanks for the help man. :)
– Waqar Danish
Nov 25 '18 at 16:54
No problem ;) my pleasure.
– Matthieu Brucher
Nov 25 '18 at 16:57