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))
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'm working on a convolutional neural network and I get the error
AttributeError: module 'keras.utils.generic_utils' has no attribute
'populate_dict_with_module_objects
here is the code excluding the data processing on the dataset.
my version for Keras is 2.4.3, TensorFlow is 2.5.0 and python is 3.8
I've seen many other people facing similar issues but I believe it has to do with compatibility versions of TensorFlow. I have tried like 3-4 versions of TensorFlow but they all gave me obscure errors like the one above.
I hope this can be fixed because otherwise, I don't know how I will continue with the network
import pickle
import time
import os
import cv2
import random
import matplotlib.pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Activation
model = Sequential()
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Flatten())
model.add(Dense(128, input_shape = X.shape[1:]), activation = 'relu')
model.add(Dense(4,activation = 'softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])
model.fit(X,Y, epochs =2, validation_split = 0.2)
If you're using tensorflow 2.x, you should not mix standalone keras and new tf.keras in the same import. You above code should have imported as follows: (test on version 2.4, 2.5 in colab).
import pickle, time, os ,cv2, random
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv2D, MaxPooling2D, Dense,
Flatten, Activation)
model = Sequential()
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(2,2))
model.add(Flatten())
model.add(Dense(128, activation = 'relu'))
model.add(Dense(4,activation = 'softmax'))
Also, note that input_shape at the middle of the model doesn't have any effect (as you do in Dense layer).
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 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!")