I am running a snippet of code that is supposed to train a manuscript recognition model, in Python language in a COLAB work environment.
The code loads a database of pictures of literature written by people from MNIST and practices them.
The code:
import numpy
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt
#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]
def baseline_model():
#create model
model=Sequential()
model.add(Conv2D(32,(5,5), input_shape=(1,28,28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
#complie model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
The problem is that the code returns an error in the last line.
The error:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-111-ec22b5dcc4e3> in <module>()
63
64 model=baseline_model()
---> 65 model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on device type CPU
[[node sequential_36/max_pooling2d_21/MaxPool (defined at <ipython-input-111-ec22b5dcc4e3>:65) ]] [Op:__inference_train_function_10696]
Function call stack:
train_function
Would appreciate help,
Thank you
InvalidArgumentError: Default MaxPoolingOp only supports NHWC on
device type CPU
As the error clearly says, it can't work with NCHW (i.e. batch_size, channels, height, width) on CPU.
If you want to use CPU, then you should change the format as NHWC (i.e.batch_size, height, width, channels) then input should be (28,28,1).
One more suggestion never mix imports from tensorflow and keras.
Where as on GPU, it supports with NCHW. You can refer working code as shown below
import numpy
from tensorflow.keras.datasets import mnist # import from TF
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import utils as np_utils
from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')
from matplotlib import pyplot as plt
#load data from mnist
(xTrain,yTrain),(xTest,yTest)=mnist.load_data()
#reshape the images to be 28*28 pixels
xTrain=xTrain.reshape(xTrain.shape[0],1,28,28).astype('float32')
xTest=xTest.reshape(xTest.shape[0],1,28,28).astype('float32')
#normalize inputs from 0-255 to 0-1
xTrain=xTrain/255
xTest=xTest/255
#one hot encode outputs
yTrain=np_utils.to_categorical(yTrain)
yTest=np_utils.to_categorical(yTest)
num_classes=yTest.shape[1]
def baseline_model():
#create model
model=Sequential()
model.add(Conv2D(32,(5,5), input_shape=(1,28,28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
#complie model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model=baseline_model()
model.fit(xTrain, yTrain, validation_data=(xTest, yTest), epochs=1, batch_size=200, verbose=2)
output:
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
300/300 - 34s - loss: 0.2372 - accuracy: 0.9304 - val_loss: 0.0756 - val_accuracy: 0.9760
Related
I want to train a 1d convolutional model in the unsupervised way for denoising the 1d timeseries data.In this regard I generated the 1d noisy timeseries as mentioned in inpdata and i want to denoise it.
My code is
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense,Input,Reshape
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D,AveragePooling1D,UpSampling1D
from tensorflow.keras.models import Sequential,Model
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Dense, MaxPooling1D, Flatten
from tensorflow.keras.optimizers import Adam
inpdata=np.random.rand(128,)
print(inpdata.shape[0])
print(inpdata)
model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(128,1)))
model.add(MaxPooling1D(2))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(2))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(28, activation='softmax'))
model.compile(Adam(lr=.0001), loss='mse', metrics=['accuracy'])
model.fit(inpdata,inpdata, batch_size=32, epochs=3, validation_split=0.1)
model.summary()
But here i am getting error is ValueError: Error when checking input: expected conv1d_input to have 3 dimensions, but got array with shape (128, 1)
I cannot understand where I am doing mistake and unable to denoise.Hope experts may suggest a better solution with an example.Thanks.
i have imported data as following
from keras.datasets import cifar10
import matplotlib.pyplot as plt
(X_train,y_train),(X_test,y_test) =cifar10.load_data()
for i in range(9):
plt.subplot(330+1+i)
plt.imshow(X_train[i])
plt.show()
it works fine as it is given in result :
next i have defined following convolution neural network structure
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.constraints import max_norm
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from keras.utils import np_utils
model =Sequential()
model.add(Convolution2D(filters=32,kernel_size=3,input_shape=(3,32,32),padding='same',activation='relu',data_format='channels_first',kernel_constraint=max_norm(3)))
model.add(Dropout(0.2))
model.add(Convolution2D(filters=32,kernel_size=3,activation='relu',padding='same',kernel_constraint=max_norm(3)))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(units=512,activation='relu',kernel_constraint=max_norm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation='softmax'))
i have done all necessary compile procedures
Epochs=25
lrate =0.01
decay =lrate /epochs
sgd =SGD(learning_rate=decay,momentum=0.9,nesterov=False)
model.compile(loss='categorical_crossentropy' , optimizer='sgd', metrics=['accuracy' ])
print(model.summary())
and result is here:
after runing following code
model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=Epochs,batch_size=32, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
it gives me error :
ValueError: Input 0 of layer sequential_7 is incompatible with the layer: expected axis 1 of input shape to have value 3 but received input with shape [None, 32, 32, 3]
please help me to fix this error
The input shape in your first convolutional layer should be input_shape=(32,32,3) instead of input_shape=(3,32,32). You should also set the data_format to the default "channels_last". The reason for setting it up like this is that the shape of cifar10 images is (32,32,3) and not (3,32,32)
you should always be careful about the input shape of the first layer of CNN using Conv2D. In tensorflow the input shape parameters are given as (batch_size, img_h, img_w, channels).
In contrary to pytorch it is (batch_size, channels, img_h, img_w). So, it should be (32,32,3) not (3, 32, 32).
I have a problem with loading self pretrained keras model.
When i make predictions directly after training model everything works fine.
My code:
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
import matplotlib.pyplot as plt
import numpy as np
import os
from keras.models import load_model
"""
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.models import load_model
"""
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Reshaping the array to 4-dims so that it can work with the Keras API
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
# Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])
# Importing the required Keras modules containing model and layers
def train():
# Creating a Sequential Model and adding the layers
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=x_train,y=y_train, epochs=10)
print(model.summary())
model.save('x.h5')
def eval():
model = load_model('x.h5')
scores = model.evaluate(x_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
train()
eval()
And i got an error:
ValueError: Unknown activation function:softmax_v2
I've tried to use different tensorflow versions (1.15, 2.0, 1.5) but this changes nothing.
Any ideas what is wrong with it?
Edit:
This problem occurs only when i try to load model.
I'm new to Machine learning i got 1 example of Cat vs Dog image classification
and here is the link to it
https://pythonprogramming.net/convolutional-neural-network-kats-vs-dogs-machine-learning-tutorial/
It worked perfectly , but now when i want to implement transfer learning to it using VGG16 , it's not working
from keras.models import Sequential, Model, load_model
from keras.applications.vgg16 import VGG16
from keras import optimizers
from keras.layers import Dropout, Flatten, Dense, Activation
from keras.models import Sequential
from keras import utils
train = train_data[:-500]
test = train_data[-500:]
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
test_y = np.array([i[1] for i in test])
from keras.layers import Activation, Conv2D, Dense, Dropout, Flatten, MaxPooling2D
from keras.models import Sequential
modelvgg = VGG16(weights='imagenet', include_top=False, input_shape=(50,50,3))
type(modelvgg)
modelvgg.layers.pop()
model = Sequential()
for layer in modelvgg.layers:
model.add(layer)
for layer in model.layers:
layer.trainable = False
model.add(Dense(1, activation= 'sigmoid'))
model.compile(optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model.summary()
model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
Here is the Error i always get
I guess there's a problem i guess of how i'm fitting my module so please i need help with that
Unrecognized keyword arguments: {'n_epoch': 10, 'validation_set': ({'input': array([[[[ 41, 40, 36],
[ 43, 42, 38],
[ 43, 42, 38],
The tutorial you linked does not use keras but tflearn, no wonder the fit call does not work. A correct call with keras would be:
model.fit(X, Y, epochs=10, validation_data=(test_x, test_y))
I am using keras from tensorflow backend to detect potholes and garbage in my trained model. The output generated for detecting potholes or garbage works fine but when i give a random image of a car or bike or cat or human or building it identifies everything as a garbage class.what should i do now? what should be the optimal output for such cases. I am providig my code here.
#train
import numpy as np
np.random.seed(123) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from dataset_pothole import pothole
from keras.models import model_from_json
# 4. Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = pothole.load_data()
# 5. Preprocess input data
X_train = X_train.reshape(X_train.shape[0], 50, 50, 3)
X_test = X_test.reshape(X_test.shape[0], 50, 50, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# 6. Preprocess class labels
Y_train = np_utils.to_categorical(y_train, 2)
Y_test = np_utils.to_categorical(y_test, 2)
# 7. Define model architecture
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(50, 50, 3)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
# 8. Compile model
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# 9. Fit model on training data
model.fit(X_train, Y_train,
batch_size=32, nb_epoch=20, verbose=1)
# 10. Evaluate model on test data
score = model.evaluate(X_test, Y_test, verbose=0)
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
print('Test loss: ', score[0])
print('Test accuracy: ', score[1])
#evaluation
import numpy as np
np.random.seed(123) # for reproducibility
import keras
from keras.utils import np_utils
from keras.models import model_from_json
import os
from PIL import Image
from numpy import *
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
img = input("Please enter the test filename:")
test = array(array(Image.open(img)).flatten())
print(test.shape)
X_test = test.reshape((1, 50, 50, 3))
print(X_test.shape)
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
#loaded_model.compile(loss='categorical_crossentropy',
# optimizer='adam',
# metrics=['accuracy'])
prediction = loaded_model.predict_classes(X_test)
print(prediction)
print(loaded_model.predict(X_test))
if prediction==[1]:
print("Pothole")
elif prediction==[0]:
print("Garbage")
else:
print("Invalid Image!")