| 106 | |
| 107 | ### Creat model |
| 108 | def get_compiled_model(): |
| 109 | if not args.model_name in ['IRV2', 'ResNet50', 'DenseNet121', 'InceptionV3']: |
| 110 | raise Exception('Pre-trained network not exists. Please choose IRV2/ResNet50/DenseNet121/InceptionV3 instead') |
| 111 | else: |
| 112 | if args.model_name == 'IRV2': |
| 113 | if database == 'RadImageNet': |
| 114 | model_dir ="../RadImageNet_models/RadImageNet-IRV2-notop.h5" |
| 115 | base_model = InceptionResNetV2(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 116 | else: |
| 117 | base_model = InceptionResNetV2(weights='imagenet', input_shape=(image_size, image_size, 3),include_top=False,pooling='avg') |
| 118 | if args.model_name == 'ResNet50': |
| 119 | if database == 'RadImageNet': |
| 120 | model_dir = "../RadImageNet_models/RadImageNet-ResNet50-notop.h5" |
| 121 | base_model = ResNet50(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 122 | else: |
| 123 | base_model = ResNet50(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 124 | if args.model_name == 'DenseNet121': |
| 125 | if database == 'RadImageNet': |
| 126 | model_dir = "../RadImageNet_models/RadImageNet-DenseNet121-notop.h5" |
| 127 | base_model = DenseNet121(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 128 | else: |
| 129 | base_model = DenseNet121(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 130 | if args.model_name == 'InceptionV3': |
| 131 | if database == 'RadImageNet': |
| 132 | model_dir = "../RadImageNet_models/RadImageNet-InceptionV3-notop.h5" |
| 133 | base_model = InceptionV3(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 134 | else: |
| 135 | base_model = InceptionV3(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 136 | if args.structure == 'freezeall': |
| 137 | for layer in base_model.layers: |
| 138 | layer.trainable = False |
| 139 | if args.structure == 'unfreezeall': |
| 140 | pass |
| 141 | if args.structure == 'unfreezetop10': |
| 142 | for layer in base_model.layers[:-10]: |
| 143 | layer.trainable = False |
| 144 | y = base_model.output |
| 145 | y = Dropout(0.5)(y) |
| 146 | predictions = Dense(2, activation='softmax')(y) |
| 147 | model = Model(inputs=base_model.input, outputs=predictions) |
| 148 | adam = Adam(lr=args.lr) |
| 149 | model.compile(optimizer=adam, loss=BinaryCrossentropy(), metrics=[keras.metrics.AUC(name='auc')]) |
| 150 | return model |
| 151 | |
| 152 | |
| 153 | |