text: import os 
  #os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  import numpy as np
  from keras.models import *
  from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, UpSampling2D, Dropout, Cropping2D
  from keras.optimizers import *
  from keras.callbacks import ModelCheckpoint, LearningRateScheduler
  from keras import backend as keras
  from data import *
  
  class myUnet(object):
  
  	def __init__(self, img_rows = 512, img_cols = 512):
  
  		self.img_rows = img_rows
  		self.img_cols = img_cols
  
  	def load_data(self):
  
  		mydata = dataProcess(self.img_rows, self.img_cols)
  		imgs_train, imgs_mask_train = mydata.load_train_data()
  		imgs_test = mydata.load_test_data()
  		return imgs_train, imgs_mask_train, imgs_test
  
  	def get_unet(self):
  
  		inputs = Input((self.img_rows, self.img_cols,3))
  		
  		'''
  		unet with crop(because padding = valid) 
  
  		conv1 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(inputs)
  		print "conv1 shape:",conv1.shape
  		conv1 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv1)
  		print "conv1 shape:",conv1.shape
  		crop1 = Cropping2D(cropping=((90,90),(90,90)))(conv1)
  		print "crop1 shape:",crop1.shape
  		pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
  		print "pool1 shape:",pool1.shape
  
  		conv2 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool1)
  		print "conv2 shape:",conv2.shape
  		conv2 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv2)
  		print "conv2 shape:",conv2.shape
  		crop2 = Cropping2D(cropping=((41,41),(41,41)))(conv2)
  		print "crop2 shape:",crop2.shape
  		pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
  		print "pool2 shape:",pool2.shape
  
  		conv3 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool2)
  		print "conv3 shape:",conv3.shape
  		conv3 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv3)
  		print "conv3 shape:",conv3.shape
  		crop3 = Cropping2D(cropping=((16,17),(16,17)))(conv3)
  		print "crop3 shape:",crop3.shape
  		pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
  		print "pool3 shape:",pool3.shape
  
  		conv4 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool3)
  		conv4 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv4)
  		drop4 = Dropout(0.5)(conv4)
  		crop4 = Cropping2D(cropping=((4,4),(4,4)))(drop4)
  		pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
  
  		conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(pool4)
  		conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv5)
  		drop5 = Dropout(0.5)(conv5)
  
  		up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5))
  		merge6 = concatenate([crop4,up6],  axis = 3)
  		conv6 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge6)
  		conv6 = Conv2D(512, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv6)
  
  		up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6))
  		merge7 = concatenate([crop3,up7],  axis = 3)
  		conv7 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge7)
  		conv7 = Conv2D(256, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv7)
  
  		up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7))
  		merge8 = concatenate([crop2,up8],  axis = 3)
  		conv8 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge8)
  		conv8 = Conv2D(128, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv8)
  
  		up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8))
  		merge9 = concatenate([crop1,up9],  axis = 3)
  		conv9 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(merge9)
  		conv9 = Conv2D(64, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv9)
  		conv9 = Conv2D(2, 3, activation = 'relu', padding = 'valid', kernel_initializer = 'he_normal')(conv9)
  		'''
  
  		conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
  		print ("conv1 shape:",conv1.shape)
  		conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
  		print ("conv1 shape:",conv1.shape)
  		pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
  		print ("pool1 shape:",pool1.shape)
  
  		conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
  		print ("conv2 shape:",conv2.shape)
  		conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
  		print ("conv2 shape:",conv2.shape)
  		pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
  		print ("pool2 shape:",pool2.shape)
  
  		conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
  		print ("conv3 shape:",conv3.shape)
  		conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
  		print ("conv3 shape:",conv3.shape)
  		pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
  		print ("pool3 shape:",pool3.shape)
  
  		conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
  		conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)
  		drop4 = Dropout(0.5)(conv4)
  		pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
  
  		conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
  		conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
  		drop5 = Dropout(0.5)(conv5)
  
  		up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5))
  		merge6 = concatenate([drop4,up6], axis = 3)
  		conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)
  		conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)
  
  		up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6))
  		merge7 = concatenate([conv3,up7], axis = 3)
  		conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)
  		conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)
  
  		up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7))
  		merge8 = concatenate([conv2,up8], axis = 3)
  		conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)
  		conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)
  
  		up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8))
  		merge9 = concatenate([conv1,up9], axis = 3)
  		conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)
  		conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
  		conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
  		conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)
  
  		model = Model(input = inputs, output = conv10)
  
  		model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy'])
  
  		return model
  
  
  	def train(self):
  
  		print("loading data")
  		imgs_train, imgs_mask_train, imgs_test = self.load_data()
  		print("loading data done")
  		model = self.get_unet()
  		print("got unet")
  
  		model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss',verbose=1, save_best_only=True)
  		print('Fitting model...')
  		model.fit(imgs_train, imgs_mask_train, batch_size=4, nb_epoch=10, verbose=1,validation_split=0.2, shuffle=True, callbacks=[model_checkpoint])
  
  		print('predict test data')
  		imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)
  		np.save('unet/data/results/imgs_mask_test.npy', imgs_mask_test)
  
  	def save_img(self):
  
  		print("array to image")
  		imgs = np.load('unet/data/results/imgs_mask_test.npy')
  		for i in range(imgs.shape[0]):
  			img = imgs[i]
  			img = array_to_img(img)
  			img.save("unet/data/results/%d.jpg"%(i))
  
  
  
  
  if __name__ == '__main__':
  	myunet = myUnet()
  	myunet.train()
  	myunet.save_img()
  
  
  
  
  
  
  
  
  