Guest User

Untitled

a guest
Feb 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. ```
  2. def ConvolutionNetworks(kernel_size=3, stride_size=2):
  3. def conv(model):
  4. model = Conv2D(24, (9, 9), strides=(stride_size, stride_size),activation='relu',input_shape=(100, 100, 3), data_format='channels_last')(model)
  5. model = BatchNormalization()(model)
  6. model = Conv2D(24, (7, 7), strides=(stride_size, stride_size),activation='relu')(model)
  7. model = BatchNormalization()(model)
  8. model = Conv2D(24, (kernel_size, kernel_size), strides=(stride_size, stride_size),activation='relu')(model)
  9. model = BatchNormalization()(model)
  10. model = Conv2D(24, (5, 5), strides=(1, 1),activation='relu')(model)
  11. model = BatchNormalization()(model)
  12. return model
  13. return conv
  14. ```
  15.  
  16. ```
  17. def build_tag(conv):
  18. d = K.int_shape(conv)[2]
  19. tag = np.zeros((d,d,2))
  20. print("tagging in process")
  21. for i in range(d):
  22. for j in range(d):
  23. tag[i,j,0] = float(int(i%d))/(d-1)*2-1
  24. tag[i,j,1] = float(int(j%d))/(d-1)*2-1
  25. tag = K.variable(tag)
  26. tag = K.expand_dims(tag, axis=0)
  27. batch_size = K.shape(conv)[0]
  28. tag = K.tile(tag, [batch_size,1,1,1])
  29. print("tagging done")
  30. return Input(tensor=tag)
  31. ```
  32.  
  33. ```
  34. visual_scene = Input((100, 100, 3))
  35.  
  36. visual_conv = ConvolutionNetworks()(visual_scene)
  37. tag = build_tag(visual_conv)
  38. visual_conv = Concatenate()([tag, visual_conv])
  39. visual_RN = RelationNetworks(visual_conv)
  40. visual_out = Dense(4, activation='softmax')(visual_RN)
  41. VisualModel = Model(inputs=[tag,visual_scene], outputs=visual_out)
  42. print("model made")
  43. ```
  44.  
  45. ```
  46. try:
  47. parallel_model = multi_gpu_model(VisualModel, cpu_merge=True,
  48. cpu_relocation=True,gpus=2)
  49. print("Training using multiple GPUs..")
  50. except:
  51. parallel_model = model
  52. print("Training using single GPU or CPU..")
  53. ```
  54.  
  55. ```
  56. datagen=ImageDataGenerator(rescale=1./255)
  57. train_generator=datagen.flow_from_dataframe(dataframe=training_df,
  58. directory=image_dir, x_col="image", y_col="lesion",
  59. class_mode="categorical",target_size=(IMG_SIZE,IMG_SIZE),
  60. batch_size=batchsize,shuffle=True)
  61. ```
  62.  
  63. ```
  64. parallel_model.fit_generator(generator = train_generator,
  65. steps_per_epoch = (training_df.shape[0])//batchsize,
  66. validation_data = validation_generator,
  67. validation_steps = (validation_df.shape[0])//batchsize,
  68. epochs = 30,verbose=1,callbacks=
  69. [checkpoint, csv_logger,tensorboard],
  70. use_multiprocessing=True,workers=workers)
Add Comment
Please, Sign In to add comment