I am trying to use Transfer Learning using ResNet-50 in TensorFlow2 and Keras on CIFAR-10 dataset which has (32, 32, 3) images.
The default ResNet-50's first conv layer uses a filter size of (7, 7) with stride = 2, the resulting CIFAR-10 is reduced too much spatially here which is to be avoided. As a 'hack', the images are attempted to be upscaled from (32, 32) to (224, 224). The code is:
import tensorflow.keras as K
# Define KerasTensor as input-
input_t = K.Input(shape = (32, 32, 3))
res_model = K.applications.ResNet50(
include_top = False,
weights = "imagenet",
input_tensor = input_t
)
# Since CIFAR-10 dataset is small as compared to ImageNet, the images are upscaled to (224, 224)-
to_res = (224, 224)
model = K.models.Sequential()
model.add(K.layers.Lambda(lambda image: tf.image.resize(image, to_res)))
model.add(res_model)
model.add(K.layers.Flatten())
model.add(K.layers.BatchNormalization())
model.add(K.layers.Dense(units = 10, activation = 'softmax'))
# Choose an optimizer and loss function for training-
loss_fn = tf.keras.losses.CategoricalCrossentropy()
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.1, momentum = 0.9)
model.compile(
# loss = 'categorical_crossentropy',
loss = loss_fn,
# optimizer = K.optimizers.RMSprop(lr=2e-5),
optimizer = optimizer,
metrics=['accuracy']
)
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, epochs = 10,
validation_data = (X_test, y_test),
# callbacks=[check_point]
)
To which I get the error:
Epoch 1/10 WARNING:tensorflow:Model was constructed with shape (None,
32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32,
32, 3), dtype=tf.float32, name='input_1'), name='input_1',
description="created by layer 'input_1'"), but it was called on an
input with incompatible shape (None, 224, 224, 3).
ValueError Traceback (most recent call
last)
in ()
2 x = X_train, y = y_train,
3 batch_size = batch_size, epochs = 10,
----> 4 validation_data = (X_test, y_test),
5 # callbacks=[check_point]
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py
in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
ValueError: Input 0 is incompatible with layer resnet50: expected
shape=(None, 32, 32, 3), found shape=(None, 224, 224, 3)
The input of the model is still (32, 32, 3)
input_t = K.Input(shape = (32, 32, 3))
Related
My sample CNN code looks below:
classifier = Sequential()
#1st Conv layer
classifier.add(Convolution2D(64, (9, 9), input_shape=(64, 64, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(4,4)))
#2nd Conv layer
classifier.add(Convolution2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.1))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.2))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 2, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('D:/regionGrowing_MLT/png_orig_imgs/Training',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('D:/regionGrowing_MLT/png_orig_imgs/Test',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical'
)
probs=classifier.fit(x = training_set, validation_data = test_set, epochs = 50)
I tried the following line to find the ROC curve, but i get an error message:
predictions = classifier.predict(test_set)
fpr, tpr,threshold = roc_curve(test_set,predictions)
The following error message is displayed:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-52-2ea53b1ba7f1> in <module>
----> 1 fpr, tpr,threshold = roc_curve(test_set,predictions)
ValueError: Expected array-like (array or non-string sequence), got <keras.preprocessing.image.DirectoryIterator object at 0x000002D21D1B61C0>
Any suggestions would be appreciated.
Emm! From the error, I think you have to change keras.processing image object to array. Try this I think this will help you out.
Accuracy
fil_acc_orig = accuracy_score(y_test, predictions.to_array())
ROC Curve
fil_acc_orig = roc_curve(y_test, predictions.to_array())
I try to run the following programe for images classification problem in Pytorch:
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import torch.utils.data as data
# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001
TRAIN_DATA_PATH = "train/"
TEST_DATA_PATH = "test/"
TRANSFORM_IMG = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] )
])
train_dataset = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=TRANSFORM_IMG)
train_loader = data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
test_dataset = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=TRANSFORM_IMG)
test_loader = data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
# Convolutional neural network (two convolutional layers)
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc = nn.Linear(7 * 7 * 32, num_classes)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out
model = ConvNet(num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))
# Test the model
model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
# Save the model checkpoint
torch.save(model.state_dict(), 'model/model.ckpt')
But I get a RuntimeError:
Traceback (most recent call last):
RuntimeError: Given groups=1, weight of size 16 1 5 5, expected input[100, 3, 256, 256] to have 1 channels, but got 3 channels instead
Someone could help to fix the bug? Thanks a lot.
Reference related:
https://discuss.pytorch.org/t/given-groups-1-weight-16-1-5-5-so-expected-input-100-3-64-64-to-have-1-channels-but-got-3-channels-instead/28831/17
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] to have 3 channels, but got 1 channels instead
Your input layer self.layer1 starts with a 2d convolution nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2). This conv layer expects an input with two spatial dimensions and one channel, and outputs a tesnor with the same spatial dimensions and 16 channels.
However, your input has three channels and not one (RGB image instead of gray level image).
Make sure your net and data are in synch.
I'm using Python 3 in Google Colab. I keep getting the error "FileNotFoundError: [Errno 2] No such file or directory" even if I'm pretty sure that the path I placed in was correct and verified that the folders are there.
I've used "pip install keras" and "pip install tensorflow" initially. I've tried using \ instead of \ for the path.
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('E:\Project\dataset\training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('E:\Project\dataset\test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 25,
validation_data = test_set,
validation_steps = 2000)
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('E:\Project\dataset\single_prediction\cat_or_dog_1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'dog'
else:
prediction = 'cat'
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-757ca18e13d5> in <module>()
30 target_size = (64, 64),
31 batch_size = 32,
---> 32 class_mode = 'binary')
33 test_set = test_datagen.flow_from_directory('E:\Project\dataset\test_set',
34 target_size = (64, 64),
1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/directory_iterator.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
104 if not classes:
105 classes = []
--> 106 for subdir in sorted(os.listdir(directory)):
107 if os.path.isdir(os.path.join(directory, subdir)):
108 classes.append(subdir)
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\Project\\dataset\training_set'
I think you cannot directly access files from your local machine in Google colab notebook. You have to upload your files to Colab either through Google Drive or direct Upload from your local computer.
You can refer this link for more details.
I'm trying to train a model Keras but I'm having a problem:
g = ImageDataGenerator(featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=45,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
validation_split=validation_split,
preprocessing_function=lambda x: x / 127 - 1)
g_train = g.flow(x_train, y_train,
batch_size=batch_size,
subset='training')
g_valid = g.flow(x_train, y_train,
batch_size=batch_size,
shuffle=False,
subset='validation')
history = network.fit_generator(g_train,
steps_per_epoch=len(x_train) / 32,
epochs=epochs)
ValueError: Error when checking target: expected predictions to have 4 dimensions, but got array with shape (256, 1)
Someone have any idea why? It seems much like the example in documentation to me.
x_train.shape
(50000, 32, 32, 1)
y_train.shape
(50000, 1, 1)
I meet exactly the same question as you. I also followed the guide of #td2014 but finally an error appears. My input shape is (24443, 124, 30), my lstm layer is set as follows:
model = Sequential()
model.add(LSTM(4, input_shape = (1, 30), return_sequences = True))
model.add(Dense(1))
model.add(Activation('softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam')
model.fit(X_train, y_train, epoch = 1, batch_size = 124, verbose = 2)
The error I get is "Error when checking input : expected lstm_4_input have shape (None, 1, 30) but got array with shape (24443, 124, 30)"
Do you have some suggestions for that?