尝试评估测试数据时,Tensorflow模型错误

时间:2019-06-28 10:41:05

标签: python python-3.x tensorflow machine-learning keras

我有一个给定的图像,在该图像中我必须检测一个特定的物体(以我的情况为例)一个衣架。我运行我的程序,它可以100%的准确度运行,但是当我尝试通过测试对其进行评估时,它会抛出一个错误。我是tensorflow的新手,请引导我通过正确的方向,并让我知道我在哪里做错了。将不胜感激

这是我的以下代码:

import tensorflow as tf
import matplotlib.pyplot as plt
import os 
import cv2
import numpy as np
from tqdm import tqdm 
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
import pickle
import time

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

DATADIR = "C:/Users/Desktop/Image_Classification/viewtique_dataset_Testing"

training_data = []
IMG_SIZE = 50

def create_training_data():


    for img in os.listdir(DATADIR):
        try:
            img_array = cv2.imread(os.path.join(DATADIR,img),cv2.IMREAD_GRAYSCALE)
            new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
            training_data.append([new_array]) 
            plt.imshow(img_array , cmap = 'gray')
            #plt.show()
        except Exception as e:  # in the interest in keeping the output clean...
                pass

create_training_data()

#print(len(training_data))

X = []
y =[]
for features in training_data:
    X.append(features)

X = np.array(X)
print(X[0].reshape(-1, IMG_SIZE, IMG_SIZE, 1))

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



data=np.array(training_data)
#print(data.shape)

dense_layers = [0,1,2]
layer_sizes = [32,64,128]
conv_layers = [1,2,3]



x_train, x_test = train_test_split(X, test_size=0.5)

x_train, x_test = x_train / 255.0, x_test / 255.0


for dense_layer in dense_layers:
        for layer_size in layer_sizes:
                for conv_layer in conv_layers:
                        NAME = "{}-conv-{}-nodes-{}-dense-{}".format(conv_layer,layer_size,dense_layer,int(time.time()))
                        print(NAME)
                        tensorboard = TensorBoard(log_dir=".\logs\{}".format(NAME))


                        model = Sequential()

                        model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
                        model.add(Activation('relu'))
                        model.add(MaxPooling2D(pool_size=(2, 2)))

                        for l in range(conv_layer-1):
                                model.add(Conv2D(layer_size, (3, 3)))
                                model.add(Activation('relu'))
                                model.add(MaxPooling2D(pool_size=(2, 2)))


                        model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors


                        for l in range(dense_layer):
                                model.add(Dense(layer_size))
                                model.add(Activation('relu'))

                        model.add(Dense(1))
                        model.add(Activation('sigmoid'))

                        model.compile(loss='binary_crossentropy',
                                      optimizer='adam',
                                      metrics=['accuracy'])

                        model.fit(x_train, epochs=5,callbacks=[tensorboard])
                        #model.fit(X, y,batch_size=32,epochs=10,validation_split=0.3,callbacks=[tensorboard])
                        #model.fit(x_train,x_test, validation_split =0.1,epochs=10)

                        model.evaluate(x_test)

如果我删除了model.evaluate(x_test),它可以使用纪元,并为我提供了所需的精度。但是当我包含model.evaluate(x_test)时,它将抛出

tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: You must feed a value for placeholder tensor 'activation_1_target' with dtype float and shape [?,?]
         [[{{node activation_1_target}}]]
         [[loss/mul/_77]]
  (1) Invalid argument: You must feed a value for placeholder tensor 'activation_1_target' with dtype float and shape [?,?]
         [[{{node activation_1_target}}]]
0 successful operations.
0 derived errors ignored.

我想了解验证和测试之间的区别,以及将多少数据拆分为验证和测试。

我的所有组合都无法运行,并且会引发一些资源错误:

ResourceExhaustedError: OOM when allocating tensor with shape[147456,128] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
     [[{{node Adam_25/Adam/update_dense_47/kernel/ResourceApplyAdam}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

0 个答案:

没有答案