How can I use transfer learning for a Keras regression problem?

transfer learning keras
using pre-trained deep learning models for your own dataset
advantages of transfer learning
transfer learning python
transfer learning image classification
transfer learning tutorial
resnet transfer learning keras
transfer learning applications

I am trying to build a CNN using transfer learning and fine tuning. The task is to build a CNN with Keras getting a dataset of images (photos of houses) and CSV file (photos names and prices), and train CNN with these inputs. But I have a problem that I cannot fix.

This is my code:

import pandas as pd
from google.colab import drive
from sklearn.model_selection import train_test_split
from keras import applications
from keras import optimizers
from keras import backend
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, load_model
from keras.layers import GlobalAveragePooling2D, Dense, Flatten
from matplotlib import pyplot

drive.mount('/content/gdrive')
!unzip -n '/content/gdrive/My Drive/HOUSEPRICES.zip' >> /dev/null

data_path = 'HOUSEPRICES/'
imgs_path = data_path + "images/"
labels_path = data_path + "prices.csv"

labels = pd.read_csv(labels_path), dtype = {"prices": "float64"})

seed = 0
train_data, test_data = train_test_split(labels, test_size=0.25, random_state=seed) 
dev_data, test_data = train_test_split(test_data, test_size=0.5, random_state=seed)  

train_data = train_data.reset_index(drop=True)
dev_data = dev_data.reset_index(drop=True)
test_data = test_data.reset_index(drop=True)

datagen = ImageDataGenerator(rescale=1./255)

img_width = 320
img_height = 240  
x_col = 'image_name'          
y_col = 'prices'


batch_size = 64              
train_dataset = datagen.flow_from_dataframe(dataframe=train_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                            class_mode="input", target_size=(img_width,img_height), batch_size=batch_size)
dev_dataset = datagen.flow_from_dataframe(dataframe=dev_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                          class_mode="input",target_size=(img_width,img_height), batch_size=batch_size)
test_dataset = datagen.flow_from_dataframe(dataframe=test_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                           class_mode="input", target_size=(img_width,img_height), batch_size=batch_size)


base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width,img_height,3))


for layer in base_model.layers:
    layer.trainable = False   

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Flatten()(x)
x = Dense(512, activation='relu')(x)

predictions = Dense(1, activation='linear')(x) 

model = Model(inputs=[base_model.input], outputs=[predictions])
model.summary()   

model.compile(loss='mse',     
              optimizer=optimizers.adam(lr=1e-5),  
              metrics=['mse'])


model.fit_generator(train_dataset,
                    epochs=20,  
                    verbose=2,  
                    steps_per_epoch=len(train_data)/batch_size,
                    validation_data=dev_dataset,
                    validation_steps=len(dev_data)/batch_size)

test_loss, test_mse = model.evaluate_generator(test_dataset,                                                   steps=len(test_data)/batch_size, verbose=1)

And I get this error:

ValueError: Input 0 is incompatible with layer flatten_9: expected min_ndim=3, found ndim=2

What is the problem with my code? Probably I am not building the dataset (images + numerical prices) properly? Or it has a problem with the model architecture? How can I fix the code?

Flatten(), converts higher dimensional vectors into 2 dimensional. If you already have a 2 dimensional vector, then you don't need Flatten().

How to Improve Performance With Transfer Learning for Deep , How to use transfer learning to improve the performance of an MLP for a multiclass Updated Oct/2019: Updated for Keras 2.3 and TensorFlow 2.0. We will use a small multi-class classification problem as the basis to� Transfer Learning in Keras with Computer Vision Models. By Jason Brownlee on May 15, 2019 in Deep Learning for Computer Vision. Last Updated on September 3, 2019. Deep convolutional neural network models may take days or even weeks to train on very large datasets. A way to short-cut this process is to re-use the model weights from pre-trained models that were developed for standard computer vision benchmark datasets, such as the ImageNet image recognition tasks.

import pandas as pd
from google.colab import drive
from sklearn.model_selection import train_test_split
from keras import applications
from keras import optimizers
from keras import backend
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, load_model
from keras.layers import GlobalAveragePooling2D, Dense, Flatten
from matplotlib import pyplot

drive.mount('/content/gdrive')
!unzip -n '/content/gdrive/My Drive/HOUSEPRICES.zip' >> /dev/null

data_path = 'HOUSEPRICES/'
imgs_path = data_path + "images/"
labels_path = data_path + "prices.csv"

labels = pd.read_csv(labels_path), dtype = {"prices": "float64"})

seed = 0
train_data, test_data = train_test_split(labels, test_size=0.25, random_state=seed) 
dev_data, test_data = train_test_split(test_data, test_size=0.5, random_state=seed)  

train_data = train_data.reset_index(drop=True)
dev_data = dev_data.reset_index(drop=True)
test_data = test_data.reset_index(drop=True)

datagen = ImageDataGenerator(rescale=1./255)

img_width = 320
img_height = 240  
x_col = 'image_name'          
y_col = 'prices'


batch_size = 64              
train_dataset = datagen.flow_from_dataframe(dataframe=train_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                            class_mode="other", target_size=(img_width,img_height), batch_size=batch_size)
dev_dataset = datagen.flow_from_dataframe(dataframe=dev_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                          class_mode="other",target_size=(img_width,img_height), batch_size=batch_size)
test_dataset = datagen.flow_from_dataframe(dataframe=test_data, directory=imgs_path, x_col=x_col, y_col=y_col, has_ext=True,
                                           class_mode="other", target_size=(img_width,img_height), batch_size=batch_size)


base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width,img_height,3))


for layer in base_model.layers:
    layer.trainable = False   

x = base_model.output
x = GlobalAveragePooling2D()(x)    
x = Dense(256, activation='relu')(x)
x = Dropout(0.4)(x)
x = Dense(256, activation='relu')(x)

predictions = Dense(1, activation='linear')(x) 

model = Model(inputs=[base_model.input], outputs=[predictions])
model.summary()   

model.compile(loss='mse',     
              optimizer=optimizers.adam(lr=1e-5),  
              metrics=['mse'])


model.fit_generator(train_dataset,
                    epochs=20,  
                    verbose=2,  
                    steps_per_epoch=len(train_data)/batch_size,
                    validation_data=dev_dataset,
                    validation_steps=len(dev_data)/batch_size)

test_loss, test_mse = model.evaluate_generator(test_dataset, steps=len(test_data)/batch_size, verbose=1)

A Comprehensive Hands-on Guide to Transfer Learning with Real , All examples will be covered in Python using keras with a tensorflow backend, In the case of problems in the computer vision domain, certain In their paper, A Survey on Transfer Learning, Pan and Yang use (for classification/regression), but we also selectively retrain some of the previous layers. Transfer learning refers to the technique of using knowledge of one domain to another domain.i.e. a Neural Network model trained on one data-set can be used for other data-set by fine-tuning the

GlobalAveragePooling2D does pooling over the spatial data. The output shape is (batch_size, channels). So, this can be directly fed to a Dense layer without the need for a Flatten. To fix the code, remove this line:

x = Flatten()(x) 

Refer this link for more examples on how to fine-tune your network.

https://keras.io/applications/

class_mode="input" is for auto encoders; that is why there was an error about the target not having the same shape as input.

class_mode = 'other' works because y_col is defined.

https://keras.io/preprocessing/image/#flow_from_dataframe

Transfer Learning with Keras and Deep Learning, In this tutorial you will learn how to perform transfer learning (for image learning, we could treat these challenges as two separate problems: 25,088- dim, can be trained in a few seconds using a Logistic Regression model. As a rule of thumb, when we have a small training set and our problem is similar to the task for which the pre-trained models were trained, we can use transfer learning. If we have enough data, we can try and tweak the convolutional layers so that they learn more robust features relevant to our problem. You can get a detailed overview of Fine-tuning and transfer learning here. We will discuss Transfer Learning in Keras in this post. ImageNet Jargon

Perform regression, using transfer learning, to predict house prices , Perform regression, using transfer learning, to predict house prices knowledge gained from solving one problem and applying it to solve some different but related problem. Keras, Regression, and CNNs - PyImageSearch� Transfer learning via feature extraction. Transfer learning via fine-tuning. When performing feature extraction, we treat the pre-trained network as an arbitrary feature extractor, allowing the input image to propagate forward, stopping at pre-specified layer, and taking the outputs of that layer as our features.

Tutorial: Transfer Learning Using Keras | by Shruti Jadon, Transfer learning refers to the technique of using knowledge of one domain to have learned features that are relevant to our classification problem. and then using this forward pass data as input to logistic regression for� In this tutorial, you will learn how to perform regression using Keras and Deep Learning. You will learn how to train a Keras neural network for regression and continuous value prediction, specifically in the context of house price prediction. Today’s post kicks off a 3-part series on deep learning, regression, and continuous value prediction.

Basic regression: Predict fuel efficiency, In a regression problem, we aim to predict the output of a continuous value, like a price or a probability. Contrast this with This example uses the tf.keras API, see this guide for details. # Use The dataset is available from the UCI Machine Learning Repository. This normalized data is what we will use to train the model. If you are trying to use transfer-learning using custom model, the answer depends on the way you saved your model architecture(description) and weights. 1. If you saved the description and weights of the model on single .h5 file. You can easily load model, using keras's load_model method.

Comments
  • Thank you. If I remove this line, I get this error. ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (64, 640, 480, 3)
  • Thank you. If I remove this line, I get this error. ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (64, 640, 480, 3)
  • That cannot be. Can you please post the updated code?
  • Updated. I think the problem could be the class_mode="input", instead I use class_mode="other" and it works fine!
  • Ok, this is a different problem than mentioned in your question. I updated the answer to include why 'other' works.