Keras incompatible input - tensorflow

I am using Keras to make a prediction model. But when I try to get the prediction, I get a ValueError indicating that the input is incompatible. Here is a minimal code that is reproducing the issue:
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard
import random
import time
import numpy as np
import pickle
#Bring data:
Name = "Predict-{}".format(int(time.time()))
tensorboard = TensorBoard(log_dir='logs/{}'.format(Name))
pickle_in = open("training_data.pickle","rb")
training_data = pickle.load(pickle_in)
random.shuffle(training_data)
X=[]
y=[]
for file_data, categ in training_data :
X.append(file_data)
y.append(categ)
X=np.array(X)
y=np.array(y)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(172,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(2,activation=tf.nn.softmax))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(X,y,epochs=10, batch_size=128,validation_split=0.25, callbacks=[tensorboard])
#New input Xnew, with len(Xnew)=172
Xnew = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]
ynew = model.predict(Xnew)
Here is the error I got :
ValueError: Input 0 of layer dense_107 is incompatible with the layer: expected axis -1 of input shape to have value 172 but received input with shape [None, 1]
The model summary :
Model: "sequential_53"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_53 (Flatten) (None, 172) 0
_________________________________________________________________
dense_109 (Dense) (None, 172) 29756
_________________________________________________________________
dense_110 (Dense) (None, 2) 346
=================================================================
Total params: 30,102
Trainable params: 30,102
Non-trainable params: 0
_________________________________________________________________
I also don't understand why the shape is (None, 172) and not 172.

The error lies on the object you give to the network in order to predict. It expects an array as (batch, data), you are giving a (,data).
What you could do is just saying to the network "here I have one batch"
Xnew = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
ynew = model.predict(np.expand_dims(Xnew, axis=0))
expand_dims add a dimension (in this case in front of the shape)
And for the last part, you get (None, 172) None means that we don't know the number of batch in the summary (you can specify it if you want), the network is expecting (X, 172), with X that can be everything (32, 64 ...)

There is no need for flatten layer in the model as your data is already in the correct shape for Dense layer.
So you can re-write the model like this,
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(input_shape=(172,), units=172, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(2,activation=tf.nn.softmax))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
Note the input_shape parameter in the first Dense layer. It tells the model how much dimensions to expect.
Model summary will look like this,
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_4 (Dense) (None, 172) 29756
_________________________________________________________________
dense_5 (Dense) (None, 2) 346
=================================================================
Total params: 30,102
Trainable params: 30,102
Non-trainable params: 0
_________________________________________________________________
Training model using random data of same shape as your data,
X = np.random.rand(*(602, 172))
y = np.random.rand(*(602, 2))
model.fit(X, y, epochs=100, batch_size=32)
Epoch 1/100
19/19 [==============================] - 0s 2ms/step - loss: 0.6006 - accuracy: 0.9934
Epoch 2/100
19/19 [==============================] - 0s 5ms/step - loss: 0.6006 - accuracy: 0.9900
Epoch 3/100
19/19 [==============================] - 0s 1ms/step - loss: 0.6006 - accuracy: 0.9983
Regarding None in the output shape, None means this dimension is variable.
The first dimension in a keras model is always the batch size. You don't need fixed batch sizes, unless in very specific cases (for instance, when working with stateful=True LSTM layers).

Related

How to change tensorflow interpreter dtype from tf.float32?

I am attempting to quantize a model that is being used as a chess engine.
the input is a np array of ints
array([[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],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, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 1, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 1, 1, 0],
[1, 0, 1, 0, 1, 1, 0, 1]],
[[1, 0, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]], dtype=int16)
with the output giving an evaluation of the board between 0 and 1. I would like to quantize this but no matter what I add to the converter I cannot change the dtype of
interpreter.get_output_details()[0]['dtype']
from tf.float32
def representative_dataset():
for y in y_train:
yield {
"eval": y,
}
keras_model = tf.keras.models.load_model('model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
#converter.representative_dataset = representative_dataset #if not commente
converter.inference_input_type = tf.uint8
#should only quantize the fixed params like weights
tflite_quant_model = converter.convert()
If I only have the line: converter.inference_input_type = tf.uint8
I get the error ValueError: The inference_input_type and
inference_output_type must be tf.float32.
If I have the line
converter.representative_dataset = representative_dataset
and I use tf.uint8 I get the error
KeyError: 'input_1'
with the traceback
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18712/1100160250.py in <module>
6 #should only quantize the fixed params like weights
7
----> 8 tflite_quant_model = converter.convert()
packages\tensorflow\lite\python\lite.py in wrapper(self, *args, **kwargs)
931 def wrapper(self, *args, **kwargs):
932 # pylint: disable=protected-access
--> 933 return self._convert_and_export_metrics(convert_func, *args, **kwargs)
934 # pylint: enable=protected-access
935
packages\tensorflow\lite\python\lite.py in _convert_and_export_metrics(self, convert_func, *args, **kwargs)
909 self._save_conversion_params_metric()
910 start_time = time.process_time()
--> 911 result = convert_func(self, *args, **kwargs)
912 elapsed_time_ms = (time.process_time() - start_time) * 1000
913 if result:
-packages\tensorflow\lite\python\lite.py in convert(self)
1340 Invalid quantization parameters.
1341 """
-> 1342 saved_model_convert_result = self._convert_as_saved_model()
1343 if saved_model_convert_result:
1344 return saved_model_convert_result
packages\tensorflow\lite\python\lite.py in _convert_as_saved_model(self)
1322 self._convert_keras_to_saved_model(temp_dir))
1323 if self.saved_model_dir:
-> 1324 return super(TFLiteKerasModelConverterV2,
1325 self).convert(graph_def, input_tensors, output_tensors)
1326 finally:
packages\tensorflow\lite\python\lite.py in convert(self, graph_def, input_tensors, output_tensors)
1139 **converter_kwargs)
1140
-> 1141 return self._optimize_tflite_model(
1142 result, self._quant_mode, quant_io=self.experimental_new_quantizer)
1143
packages\tensorflow\lite\python\convert_phase.py in wrapper(*args, **kwargs)
213 except Exception as error:
214 report_error_message(str(error))
--> 215 raise error from None # Re-throws the exception.
216
217 return wrapper
packages\tensorflow\lite\python\convert_phase.py in wrapper(*args, **kwargs)
203 def wrapper(*args, **kwargs):
204 try:
--> 205 return func(*args, **kwargs)
206 except ConverterError as converter_error:
207 if converter_error.errors:
packages\tensorflow\lite\python\lite.py in _optimize_tflite_model(self, model, quant_mode, quant_io)
869 q_bias_type = quant_mode.bias_type()
870 q_allow_float = quant_mode.is_allow_float()
--> 871 model = self._quantize(model, q_in_type, q_out_type, q_activations_type,
872 q_bias_type, q_allow_float)
873
packages\tensorflow\lite\python\lite.py in _quantize(self, result, input_type, output_type, activations_type, bias_type, allow_float)
611 custom_op_registerers_by_func)
612 if self._experimental_calibrate_only or self.experimental_new_quantizer:
--> 613 calibrated = calibrate_quantize.calibrate(
614 self.representative_dataset.input_gen)
615
packages\tensorflow\lite\python\convert_phase.py in wrapper(*args, **kwargs)
213 except Exception as error:
214 report_error_message(str(error))
--> 215 raise error from None # Re-throws the exception.
216
217 return wrapper
packages\tensorflow\lite\python\convert_phase.py in wrapper(*args, **kwargs)
203 def wrapper(*args, **kwargs):
204 try:
--> 205 return func(*args, **kwargs)
206 except ConverterError as converter_error:
207 if converter_error.errors:
packages\tensorflow\lite\python\optimize\calibrator.py in calibrate(self, dataset_gen)
224 dataset_gen: A generator that generates calibration samples.
225 """
--> 226 self._feed_tensors(dataset_gen, resize_input=True)
227 return self._calibrator.Calibrate()
\tensorflow\lite\python\optimize\calibrator.py in _feed_tensors(self, dataset_gen, resize_input)
108 self._interpreter = Interpreter(model_content=self._model_content)
109 signature_key = None
--> 110 input_array = self._create_input_array_from_dict(None, sample)
111 elif isinstance(sample, list):
112 signature_key = None
\tensorflow\lite\python\optimize\calibrator.py in _create_input_array_from_dict(self, signature_key, inputs)
84 key=lambda item: item[1]["index"])
85 for input_name, _ in input_details:
---> 86 input_array.append(inputs[input_name])
87 return input_array
88
KeyError: 'input_1'
I was thinking this could be something wrong with my representative_dataset function but am unsure what to do here. I am really hoping to not convert my board representations to float32s

Trying to fill a zeros numpy array of size (6,6) with a array size (2,2)

I have a array S:
S = array([[980, 100],
[ 3, 5]])
I need to resize him or fill a zeros array to size (6,6). My desire output is:
out = array([[980, 100, 0, 0, 0, 0],
[3, 5, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]], dtype=int32)
Anyone can help?
I figure it out.
Create a zeros matrix to desired size matrix
zeros = np.zeros((6,6))
your array
array = np.array([[1,2,5,6],[3,4,4,3],[5,6,2,8]])
#getting shape
lenx, leny = array.shape
fill the zeros matrix with your array
zeros[:lenx,:leny] = array

how to train model in which labels is [5,30]?

How to train on a dataset which has each label of shape [5,30]. For example :
[
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54, 55, 21, 56, 57, 3,
22, 19, 58, 6, 59, 4, 60, 1, 61, 62, 23, 63, 23, 64],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 65, 7, 66, 2, 67, 68, 3, 69, 70],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 11, 12, 5, 13, 14, 9, 10, 5, 15, 16, 17, 2, 8],
[ 0, 0, 0, 0, 0, 2, 71, 1, 72, 73, 74, 7, 75, 76, 77, 3,
20, 78, 18, 79, 1, 21, 80, 81, 3, 82, 83, 84, 6, 85],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
86, 87, 3, 88, 89, 1, 90, 91, 22, 92, 93, 4, 6, 94]
]
One way was to reshape the labels to [150], but that will make the tokenized sentences lose their meanings. Please suggest me how to arrange the keras layers and which layers to be able to make the model? I want to able to generate sentences later.
My code for model right now is this.
model = tf.keras.Sequential([ feature_layer,
layers.Dense(128, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dropout(.1),
layers.Dense(5),
layers.Dense(30, activation='softmax'), ])
opt = Adam(learning_rate=0.01)
model.compile(optimizer=opt, loss='mean_absolute_percentage_error', metrics=['accuracy'])
The actual data.
state
district
month
rainfall
max_temp
min_temp
max_rh
min_rh
wind_speed
advice
Orissa
Kendrapada
february
0.0
34.6
19.4
88.2
29.6
12.0
chances of foot rot disease in paddy crop; apply urea at 3 weeks after transplanting at active tillering stage for paddy;......
Jharkhand
Saraikela Kharsawan
february
0
35.2
16.6
29.4
11.2
3.6
provide straw mulch and go for intercultural operations to avoid moisture losses from soil; chance of leaf blight disease in potato crop; .......
I need to be able to generate the advices.
If you do consider that the output needs to be in this shape (and not flattened), the easiest (and also correct solution in my opinion) is to have a multi-output network, each output having a layers.Dense(30,activation='softmax').
You would have something like:
def create_model():
base_model = .... (stacked Dense units + other) # you can even create multi-input multi-output if you really want that.
first_output = Dense(30,activation='softmax',name='output_1')(base_model)
second_output = Dense(30,activation='softmax',name='output_2')(base_model)
...
fifth_output = Dense(30,activation='softmax',name='output_5')(base_model)
model = Model(inputs=input_layer,
outputs=[first_output,second_output,third_output,fourth_output,fifth_output])
return model
optimizer = tf.keras.optimizers.Adam()
model.compile(optimizer=optimizer,
loss={'output_1': 'sparse_categorical_crossentropy',
'output_2': 'sparse_categorical_crossentropy',
'output_3': 'sparse_categorical_crossentropy',
'output_4': 'sparse_categorical_crossentropy',
'output_5': 'sparse_categorical_crossentropy'},
metrics={'output_1':tf.keras.metrics.Accuracy(),
'output_2':tf.keras.metrics.Accuracy(),
'output_3':tf.keras.metrics.Accuracy(),
'output_4':tf.keras.metrics.Accuracy(),
'output_5':tf.keras.metrics.Accuracy()})
model.fit(X, y,
epochs=100, batch_size=10, validation_data=(val_X, val_y))
Here, note that y (both for train and valid) is a a numpy array of length 5 (number of outputs) and each element has length 30.
Again, ensure that you actually need such a configuration; I posted the answer as a demonstration of multi-output label in TensorFlow and Keras and for the benefit of the others, but I am not 100% sure you actually need this exact configuration (perhaps you can opt for something easier).
Note the usage of sparse_categorical_crossentropy, since your labels are not one-hot encoded (also MAPE is for regression, not classification).

Block diagonal matrix with offsets Python

Numpy provides a way to create a diagonal matrix from single elements using offset. Now, instead of single elements, I have a list of 2*2 blocks to insert along a diagonal with a specified offset.
Below is 11 blocks of 2*2 arrays that should fit along the +1 offset of a 24*24 matrix. I am aware that scipy.linalg.block_diag can create a block diagonal for an (implicit) offset of zero.
In general, I have a list of 2*2 block arrays and I want to insert these blocks along specified offsets from the main 2*2 block diagonal
[array([[ 1, 8],[ 5, 40]]), array([[ 2, 7],[10, 35]]), array([[0, 0], [0, 0]]), array([[ 3, 6],[15, 30]]), array([[ 4, 5],[20, 25]]),array([[0, 0],[0, 0]]), array([[ 5, 4],[25, 20]]), array([[ 6, 3],[30, 15]]), array([[0, 0],[0, 0]]), array([[ 7, 2],[35, 10]]), array([[ 8, 1], [40, 5]])]
You can make block_diag create an offset by prepending and appending an array of width/height zero:
from scipy import linalg
blocks = np.multiply.outer(np.arange(1,4), np.ones((2,2), int))
offset = 3
aux = np.empty((0, offset), int)
linalg.block_diag(aux.T, *blocks, aux)
# array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 2, 2, 0, 0, 0, 0, 0],
# [0, 0, 2, 2, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 3, 3, 0, 0, 0],
# [0, 0, 0, 0, 3, 3, 0, 0, 0]])
linalg.block_diag(aux, *blocks, aux.T)
# array([[0, 0, 0, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 1, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 2, 2, 0, 0],
# [0, 0, 0, 0, 0, 2, 2, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 3, 3],
# [0, 0, 0, 0, 0, 0, 0, 3, 3],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0]])

Why does tensorflow didn't accept np.float?

I am using tensorflow to run a cnn deep learning program, but it failed? i have translated my input data 'images' to np.float32, but it still report dtype error:
E tensorflow/core/client/tensor_c_api.cc:485] You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
[[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
following is my code:
import dataset
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
class CNN(object):
def __init__(self):
self.x = tf.placeholder(tf.float32, [None, 784])
self.y_ = tf.placeholder(tf.float32, [None, 10])
# First Convolutional Layer
W_conv1 = self.weight_variable([5, 5, 1, 32])
b_conv1 = self.bias_variable([32])
x_image = tf.reshape(self.x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(self.conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = self.max_pool_2x2(h_conv1)
# Second Convolutional Layer
W_conv2 = self.weight_variable([5, 5, 32, 64])
b_conv2 = self.bias_variable([64])
h_conv2 = tf.nn.relu(self.conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = self.max_pool_2x2(h_conv2)
# Densely Connected Layer
W_fc1 = self.weight_variable([7 * 7 * 64, 1024])
b_fc1 = self.bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# Dropout
self.keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, self.keep_prob)
# Readout Layer
W_fc2 = self.weight_variable([1024, 10])
b_fc2 = self.bias_variable([10])
self.y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
# Train and Evaluate the Model
self.cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.y_conv, self.y_))
self.train_step = tf.train.AdamOptimizer(1e-4).minimize(self.cross_entropy)
self.correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y_, 1))
self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
self.saver = tf.train.Saver()
self.sess = tf.Session()
self.sess.run(tf.initialize_all_variables())
print("cnn initial finished!")
def weight_variable(self, shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(self, shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(self, x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(self, x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def train(self):
# 初始化数据集
self.trainset = dataset.Train()
# mnist_softmax.py 中使用的是在sess中通过run方法执行train_step, accuracy
# mnist_cnn.py中 使用的是直接执行train_step, accuracy.eval,所以必须要传入session参数
for i in range(20000):
batch_xs, batch_ys = mnist.train.next_batch(50)
if i%100 == 0:
#print(batch_xs[0])
#print(batch_ys[0])
self.train_accuracy = self.accuracy.eval(session=self.sess, feed_dict={self.x: batch_xs, self.y_: batch_ys, self.keep_prob: 1.0})
print("step %d, trainning accuracy %g" % (i, self.train_accuracy))
self.train_step.run(session=self.sess, feed_dict={self.x: batch_xs, self.y_: batch_ys, self.keep_prob: 0.5})
# Save the variables to disk.
save_path = self.saver.save(self.sess, "CNN_data/model.ckpt")
print("Model saved in file: %s" % save_path)
#print("test accuracy %g" % self.accuracy.eval(session=self.sess, feed_dict={self.x: mnist.test.images, self.y_: mnist.test.labels, self.keep_prob: 1.0}))
def predict(self, images):
images = np.reshape(images, (1, 784))
images = images.astype(np.float32)
print(images)
ckpt = tf.train.get_checkpoint_state("CNN_data/")
if ckpt and ckpt.model_checkpoint_path:
self.saver.restore(self.sess, ckpt.model_checkpoint_path)
else:
print("No checkpoint found!")
predictions = self.sess.run(self.y_conv, feed_dict={self.x: images})
return predictions
if __name__ == '__main__':
cnn = CNN()
#cnn.train()
images = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
cnn.predict(images)
the train method is no problem, but when call predict, there is always an error of placeholder dtype error just like above? I don't know why because i have checked that my imgaes are float32 dtype.
if it's failing here:
predictions = self.sess.run(self.y_conv, feed_dict={self.x: images})
it's because you need to pass in a self.keep_prob