Adapting cnn to image regression

Hello, is there any guide for adapting cnn to regression?
I have images and csv labels, there are demos in keras, can I do it in pytorch, the following are the adaption codes in keras, how should I do the same work in pytorch ?

from keras.applications.xception import Xception
from keras.models import Model

model = Xception(weights='imagenet', include_top=True, input_shape=(299,299, 3))

x = model.get_layer(index=len(model.layers)-2).output

print(x)
x = Dense(1)(x)

model = Model(inputs=model.input, outputs=x)
model.summary()
opt = RMSprop(lr=0.0001)
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['mae'])

You could replace the last linear layer (often called model.classifier) with a new nn.Linear layer with a single output neuron and use e.g. nn.MSELoss as the criterion.

1 Like