| 53 | |
| 54 | ### Creat model |
| 55 | def get_compiled_model(): |
| 56 | if not args.model_name in ['IRV2', 'ResNet50', 'DenseNet121', 'InceptionV3']: |
| 57 | raise Exception('Pre-trained network not exists. Please choose IRV2/ResNet50/DenseNet121/InceptionV3 instead') |
| 58 | else: |
| 59 | if args.model_name == 'IRV2': |
| 60 | if database == 'RadImageNet': |
| 61 | model_dir ="../RadImageNet_models/RadImageNet-IRV2-notop.h5" |
| 62 | base_model = InceptionResNetV2(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 63 | else: |
| 64 | base_model = InceptionResNetV2(weights='imagenet', input_shape=(image_size, image_size, 3),include_top=False,pooling='avg') |
| 65 | if args.model_name == 'ResNet50': |
| 66 | if database == 'RadImageNet': |
| 67 | model_dir = "../RadImageNet_models/RadImageNet-ResNet50-notop.h5" |
| 68 | base_model = ResNet50(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 69 | else: |
| 70 | base_model = ResNet50(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 71 | if args.model_name == 'DenseNet121': |
| 72 | if database == 'RadImageNet': |
| 73 | model_dir = "../RadImageNet_models/RadImageNet-DenseNet121-notop.h5" |
| 74 | base_model = DenseNet121(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 75 | else: |
| 76 | base_model = DenseNet121(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 77 | if args.model_name == 'InceptionV3': |
| 78 | if database == 'RadImageNet': |
| 79 | model_dir = "../RadImageNet_models/RadImageNet-InceptionV3-notop.h5" |
| 80 | base_model = InceptionV3(weights=model_dir, input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 81 | else: |
| 82 | base_model = InceptionV3(weights='imagenet', input_shape=(image_size, image_size, 3), include_top=False,pooling='avg') |
| 83 | if args.structure == 'freezeall': |
| 84 | for layer in base_model.layers: |
| 85 | layer.trainable = False |
| 86 | if args.structure == 'unfreezeall': |
| 87 | pass |
| 88 | if args.structure == 'unfreezetop10': |
| 89 | for layer in base_model.layers[:-10]: |
| 90 | layer.trainable = False |
| 91 | y = base_model.output |
| 92 | y = Dropout(0.5)(y) |
| 93 | predictions = Dense(2, activation='softmax')(y) |
| 94 | model = Model(inputs=base_model.input, outputs=predictions) |
| 95 | adam = Adam(lr=args.lr) |
| 96 | model.compile(optimizer=adam, loss=BinaryCrossentropy(), metrics=[keras.metrics.AUC(name='auc')]) |
| 97 | return model |
| 98 | |
| 99 | |
| 100 | |