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