def download_model(model_name, logger=None): dir_path = os.path.dirname(os.path.realpath(__file__)) model_path = os.path.join(dir_path, 'model') if logger is not None: logger.info('Downloading model %s... into path %s' % (model_name, model_path)) return modelzoo.download_model(args.model, os.path.join(dir_path, 'model'))
def load_pretrained_resnext_to_unext101_64_4d( ctx, need_download=False, fine_tune=False, migrate_input_norm=True, migration_list='layer_migration_list.csv'): # Note: fine-tune here is wheather tune pre-trained part if need_download: zoo.download_model('imagenet1k-resnext-101-64x4d') unext_param_list = [] resnext_params_list = [] with open(migration_list, newline='') as csvfile: list_reader = csv.reader(csvfile, delimiter=',') for row in list_reader: unext_param_list.append(row[0]) resnext_params_list.append(row[1]) if not migrate_input_norm: unext_param_list = unext_param_list[4:len(unext_param_list)] resnext_params_list = resnext_params_list[4:len(resnext_params_list)] migration_dict = dict(zip(unext_param_list, resnext_params_list)) #print(unext_param_list) #print(resnext_params_list) #print(migration_dict) #sym, arg_params, aux_params = mx.model.load_checkpoint('imagenet1k-resnext-101-64x4d', 0) model = unext.unext101_64x4d() model_params = model.collect_params() migration_params = mx.ndarray.load( 'imagenet1k-resnext-101-64x4d-0000.params') for key in migration_dict: model_params[key]._load_init(migration_params[migration_dict[key]], ctx) if not fine_tune: for param in unext_param_list: model_params[param].grad_req = 'null' # for param in model_params.values(): # param.grad_req = 'null' return model
def load_pretrained_resnext101_64_4d(ctx, need_download = True): if need_download: zoo.download_model('imagenet1k-resnext-101-64x4d') symbol_path = 'imagenet1k-resnext-101-64x4d-symbol.json' param_path = 'imagenet1k-resnext-101-64x4d-0000.params' sym = mx.sym.load(symbol_path) inter = sym.get_internals() # print(inter) new_sym = inter['flatten0_output'] inputsym = mx.sym.var('data', dtype=mx.base.mx_real_t) model = mx.gluon.nn.SymbolBlock(outputs=new_sym, inputs=inputsym) model.collect_params().load(param_path, ctx=ctx, allow_missing=False, ignore_extra=True) return model