Commit 5d50ee84 authored by Saad Jbabdi's avatar Saad Jbabdi
Browse files

Separate training from model creation plus other bits and bobs

parent 9a8106ff
......@@ -109,7 +109,7 @@ def main():
if args.empty_zone == True:
with open(outfile,'a') as f:
f.write('%s\tNaN\tNaN\n' %file)
f.write('%s\tNaN\tNaN\n' %(os.path.abspath(file)))
else:
#prepare figure and load .npy files in as an image, ready to show
fig = plt.figure()
......@@ -139,7 +139,7 @@ def main():
with open(outfile,'a') as f:
if not p.points:
f.write('%s\tNaN\tNaN\n' %file)
f.write('%s\tNaN\tNaN\n' % (os.path.abspath(file)))
else:
for point in p.points:
f.write('%20s \t %12f \t %12f \n' % (os.path.abspath(file), point[1] + w, point[0] + h))
......
#!/usr/bin/env python
# Generate models from archiectures
# Saad, 03/2019
# import modules
from keras.models import Sequential, Model
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers import Activation, Flatten, Dense, Dropout, BatchNormalization, Input, Concatenate
# Model names
# convnet_1 : our first stab at the problem
def from_name(shape,arch='convnet_1'):
if(arch=='convnet_1'):
print('BUILDING A CONVNET')
model = Sequential()
model.add(Convolution2D(20, (5, 5), strides=(1, 1), padding='valid', input_shape=shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(50, (5, 5), strides=(1, 1), padding='valid'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=4, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(500, (5, 5), strides=(2, 2), padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(2, (2, 2), strides=1, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(2, (1, 1), strides=(1, 1), padding='valid'))
model.add(Flatten())
model.add(BatchNormalization())
model.add(Activation('softmax'))
elif(arch=='shallow_inception'):
print('BUILDING A INCEPTION NET')
input_img = Input( shape=(64,64,3) ) # Input image shape hard coded for now
def inception_module(input, filters=64):
layer_1 = Convolution2D( filters, (1,1), padding='same', activation='relu')(input)
layer_1 = Convolution2D( filters, (3,3), padding='same', activation='relu')(layer_1)
layer_2 = Convolution2D( filters, (1,1), padding='same', activation='relu')(input_img)
layer_2 = Convolution2D( filters, (5,5), padding='same', activation='relu')(layer_2)
layer_3 = MaxPooling2D( (3,3), strides=(1,1), padding='same')(input_img)
layer_3 = Convolution2D( filters, (1,1), padding='same', activation='relu')(layer_3)
output = Concatenate(3)([layer_1,layer_2,layer_3])
return output
module = inception_module(input_img)
module2 = inception_module(module)
output = Flatten(name='flatten')(module2)
out = Dense(2,activation='softmax',name='dense')(output)
model = Model(inputs=input_img,outputs=out)
else:
print('Error: Unknown architecture {}'.format(arch))
return -1
return model
......@@ -21,7 +21,7 @@ from keras.preprocessing.image import ImageDataGenerator
# Other
import pandas as pd
from CellCounting.utils import db
from CellCounting.models import create_model
# ------------------------------ DATA ------------------------------ #
def prepare_data(celldb, args):
......@@ -54,32 +54,9 @@ def prepare_data(celldb, args):
# ------------------------------ MODEL ------------------------------ #
def create_model(shape,arch='convnet'):
if(arch=='convnet'):
# matconvnet model
model = Sequential()
model.add(Convolution2D(20, (5, 5), strides=(1, 1), padding='valid', input_shape=shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(50, (5, 5), strides=(1, 1), padding='valid'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=4, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(500, (5, 5), strides=(2, 2), padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(2, (2, 2), strides=1, padding='valid'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Convolution2D(2, (1, 1), strides=(1, 1), padding='valid'))
model.add(Flatten())
model.add(BatchNormalization())
model.add(Activation('softmax'))
else:
print('Error: Unknown architecture {}'.format(arch))
exit
def build_model(shape,arch='convnet_1'):
model = create_model.from_name(shape,arch)
# compile model
adam = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
......@@ -180,9 +157,8 @@ def main():
p = argparse.ArgumentParser(description='Train model on some data')
# Optional arguments
p.add_argument('--gpu', default=False, type=bool, metavar='<bool>',
help='use GPU if True (default), use CPU if False')
p.add_argument('--gpu', default=False, type=lambda s: s.lower() in ['true', 't', 'yes', '1'], metavar='<bool>',
help='try to use GPU if True (default), use CPU if False')
p.add_argument('--epochs', default=100, type=int, metavar='<int>',
help='number of training epochs (default=100)')
p.add_argument('--batch_size', default=32, type=int, metavar='<int>',
......@@ -211,6 +187,7 @@ def main():
celldb = db.CellDB()
celldb.load_from_files(args.data)
celldb.equalise_classes()
celldb.summary()
print('* Preparing and training model')
shape = celldb.images.shape[1:]
......@@ -219,7 +196,7 @@ def main():
from keras.models import load_model
model = load_model(args.load_model)
else:
model = create_model(shape,args.model)
model = build_model(shape,args.model)
info = train_model(model, celldb, args)
print('* Saving results')
......
......@@ -9,13 +9,14 @@ setup(name='FSL_CellCounting',
author_email=['saad@fmrib.ox.ac.uk','oiwi@fmrib.ox.ac.uk'],
url='<someGitLabURL',
packages=['CellCounting','CellCounting.click_cells', 'CellCounting.models','CellCounting.utils'],
install_requires=['matplotlib','numpy','tifffile','scikit_image','pandas','keras','tensorflow','sklearn','h5py','jinja2'],
#install_requires=['matplotlib','tifffile','scikit_image','pandas','keras','tensorflow-gpu','sklearn','h5py','jinja2'],
scripts=['CellCounting/click_cells/select_zones.py',
'CellCounting/click_cells/split_zones.py',
'CellCounting/click_cells/click_cells.py',
'CellCounting/click_cells/create_db.py',
'CellCounting/models/train_model.py',
'CellCounting/models/forward_density.py',
'CellCounting/models/create_model.py',
'CellCounting/utils/image_info.py',
'CellCounting/utils/cell_report.py']
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment