Created
June 29, 2019 10:24
-
-
Save emoen/00c3970ec86d2d354e4b3698f2ed7818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import numpy as np | |
| import keras | |
| from keras.layers import Dense, GlobalAveragePooling2D, GlobalMaxPooling2D, AveragePooling2D | |
| from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense,Input,BatchNormalization | |
| from keras.models import Model | |
| from keras import optimizers, layers | |
| import os | |
| from keras.preprocessing.image import ImageDataGenerator | |
| new_shape = (224, 224, 3) | |
| base_dir = '/gpfs/gpfs0/deep/data/salmon-scales/dataset_5_param' | |
| os.environ["CUDA_VISIBLE_DEVICES"]="1" | |
| a_batch_size = 12 | |
| d2015 = pd.read_csv(os.path.join(base_dir, '2015_5_param_edit.csv')) | |
| d2015 = d2015.rename(index=str, columns={'vill/oppdrett': 'vill'}) | |
| d2015.vill = d2015.vill.astype('str') | |
| d2015.at[d2015['vill']=='Vill', 'vill'] = 'vill' | |
| d2015.at[d2015['vill']=='Oppdrett', 'vill'] = 'oppdrett' | |
| d2015.at[d2015['vill']=='.', 'vill'] = 'ukjent' | |
| d2015.at[d2015['vill']=='nan', 'vill'] = 'ukjent' | |
| d2015.at[d2015['vill']=='Regnbueørret', 'vill'] = 'ukjent' | |
| d2015.at[d2015['vill']=='Utsatt', 'vill'] = 'ukjent' | |
| d2 = d2015[d2015.vill != 'ukjent'] | |
| d2 = d2.drop('smolt',axis=1) | |
| d2 = d2.drop('sjø',axis=1) | |
| d2 = d2.drop('totalt',axis=1) | |
| d2 = d2.drop('gytarar',axis=1) | |
| d2['ID nr.'] = d2['ID nr.'].astype(str) + '.jpg' | |
| d2['ID nr.'] = base_dir+'/hi2015_in_excel/' + d2['ID nr.'].astype(str) | |
| np.unique(d2.vill, return_counts=True) | |
| datagen = ImageDataGenerator( | |
| zca_whitening=False, | |
| width_shift_range=0., | |
| height_shift_range=0., #20, | |
| zoom_range=0., | |
| rotation_range=360, | |
| horizontal_flip=False, | |
| vertical_flip=True, | |
| rescale=1./255) | |
| train_generator=datagen.flow_from_dataframe( | |
| dataframe=d2, | |
| #directory=base_dir+'/hi2015_in_excel', | |
| x_col="ID nr.", | |
| y_col="vill", | |
| color_mode="rgb", | |
| subset="training", | |
| batch_size=12, | |
| seed=42, | |
| shuffle=True, | |
| class_mode="categorical", | |
| target_size=(224,224)) | |
| #scales = keras.applications.nasnet.NASNetMobile(input_shape=(224,224,3), include_top=False, weights=None, classes=2) | |
| scales = keras.applications.inception_v3.InceptionV3(input_shape=(224,224,3), include_top=False, weights=None, classes=2) | |
| z = scales.output | |
| z = GlobalMaxPooling2D()(z) | |
| z = Dense(1024)(z) | |
| z = Activation('relu')(z) | |
| z = Dense(2, input_dim=1024)(z) | |
| z = Activation('softmax')(z) | |
| scales = Model(inputs=scales.input, outputs=z) | |
| learning_rate=0.001 | |
| adam = optimizers.Adam(lr=learning_rate) | |
| for layer in scales.layers: | |
| layer.trainable = True | |
| scales.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'] ) | |
| scales.fit_generator(train_generator, steps_per_epoch=1600, epochs=150) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment