raresct / astrohack_solution

Solution to https://astrohack.org/
1 stars 1 forks source link

CNN retraining #7

Open raresct opened 7 years ago

raresct commented 7 years ago

The idea is to see whether training a new NN on top of existing features (or even the original image) works for the regression problem.

E.g. for resnet50

import numpy as np
from keras.applications import ResNet50
from keras.layers import Dense, Flatten, Dropout, Input
from keras.models import Model

def custom_fc():
    inputs = Input(shape=(1,1,2048))
    x = Flatten()(inputs)
    x = Dropout(0.5, seed=1234)(x)
    x = Dense(256, activation='relu')(x)
    x = Dropout(0.1, seed=1234)(x)
    x = Dense(32, activation='relu')(x)
    outputs = Dense(1, activation='linear')(x)
    return Model(inputs=inputs, outputs=outputs)

r50 = ResNet50(weights='imagenet',include_top=False,input_shape=(224,224,3))

x_train = np.random.random((10, 224, 224,3))
y_train = 100*np.random.random((10, 1))

x_train_r50 = r50.predict(x_train)
print x_train_r50.shape

model = custom_fc()
model.compile(optimizer='rmsprop', loss='mse')

model.fit(x_train_r50, y_train, epochs=100, batch_size=1)

y_pred = model.predict(x_train_r50)

print y_train
print '*'*30
print y_pred

Also try binning target variable.