def main():
    global_value._init()
    reload(sys)
    sys.setdefaultencoding("utf-8")
    state_dict=torch.load('/home1/sas/crop_disease_detect-master/val_eval_best.pth')
    interface = NetModule(config['model_module_name'], config['model_net_name'])
    net = interface.create_model(num_classes=config['num_classes'])
    if torch.cuda.is_available():
        net.cuda()
    net.load_state_dict(state_dict)
    net.eval()
    #print(net)
    train_loader, test_loader = get_data_loader(config)
    num=0
    f=open("/home1/sas/datasets/ai_challenger_pdr2018_validationset_20180905/AgriculturalDisease_validationset/AgriculturalDisease_validation_annotations.json",'r+')
    f_results=open("/home1/sas/datasets/ai_challenger_pdr2018_testA_20180905/AgriculturalDisease_testA/AgriculturalDisease_val_results_val_best_annotations.json",'w')
    json_data=json.load(f)
    print(len(json_data))
    for _, (image, label) in enumerate(test_loader):
        image=image.cuda()
        image=Variable(image)
        labels=net(image)
        label=torch.max(labels,1)[1].data.squeeze()
        label=int(label)
        json_data[_]['disease_class']=label
        #print(json_data[_])
        num=num+1
    dict_json=json.dumps(json_data, ensure_ascii=True, indent=2)
    f_results.write(dict_json)
    f_results.close()
    f.close()
 def __init__(self, config):
     super(ExampleModel, self).__init__(config)
     self.config = config
     self.interface = NetModule(self.config['model_module_name'],
                                self.config['model_net_name'],
                                self.config['official_source'])
     self.gpus = len(utils.gpus_str_to_list(self.config['gpu_id']))
     self.create_model()
Exemplo n.º 3
0
 def __init__(self, config):
     super(ExampleModel, self).__init__(config)
     self.config = config
     self.GPU_COUNT = utils.gpus_str_to_number(self.config['gpu_id'])
     self.ctx = [mx.gpu(i) for i in range(self.GPU_COUNT)]
     self.interface = NetModule(self.config['model_module_name'],
                                self.config['model_net_name'])
     self.create_model()
Exemplo n.º 4
0
class ExampleModel(BaseModel):
    def __init__(self, config):
        super(ExampleModel, self).__init__(config)
        self.config = config
        self.GPU_COUNT = utils.gpus_str_to_number(self.config['gpu_id'])
        self.ctx = [mx.gpu(i) for i in range(self.GPU_COUNT)]
        self.interface = NetModule(self.config['model_module_name'],
                                   self.config['model_net_name'])
        self.create_model()

    def create_model(self):
        self.net = self.interface.create_model(classes=102)
        self.net.collect_params().initialize(mx.init.Xavier(magnitude=2.24),
                                             ctx=self.ctx)

    def load(self):
        # train_mode: 0:from scratch, 1:finetuning, 2:update
        # if not update all parameters:
        # for param in list(self.net.parameters())[:-1]:    # only update parameters of last layer
        #    param.requires_grad = False
        train_mode = self.config['train_mode']

        if train_mode == 'fromscratch':
            print('from scratch...')

        elif train_mode == 'finetune':
            net = self._load()
            self.net.features = net.features
            self.net.collect_params().reset_ctx(self.ctx)
            print('finetuning...')

        elif train_mode == 'update':
            _path = os.path.join(self.config['pretrained_path'],
                                 self.config['pretrained_file'])
            self.net.load_parameters(_path, ctx=self.ctx)
            self.net.collect_params().reset_ctx(self.ctx)
            print('updating...')

        else:
            ValueError('train_mode is error...')

    def _load(self):
        if self.config['model_auto_download']:
            net = self.interface.create_model(pretrained=True)
        else:
            net = self.interface.create_model()
            _path = os.path.join(self.config['pretrained_path'],
                                 self.config['pretrained_file'])
            net.load_parameters(_path, ctx=self.ctx)
        return net
Exemplo n.º 5
0
 def __init__(self, config):
     super(ExampleModel, self).__init__(config)
     self.config = config
     self.interface = NetModule(self.config['model_module_name'], self.config['model_net_name'])
     self.create_model()
Exemplo n.º 6
0
class ExampleModel(BaseModel,object):
    def __init__(self, config):
        super(ExampleModel, self).__init__(config)
        self.config = config
        self.interface = NetModule(self.config['model_module_name'], self.config['model_net_name'])
        self.create_model()


    def create_model(self):
        self.net = self.interface.create_model(num_classes=self.config['num_classes'])#return a model
        if torch.cuda.is_available():
            self.net.cuda()


    def load(self):
        # train_mode: 0:from scratch, 1:finetuning, 2:update
        # if not update all parameters:
        # for param in list(self.net.parameters())[:-1]:    # only update parameters of last layer
        #    param.requires_grad = False
        train_mode = self.config['train_mode']
        if train_mode == 'fromscratch':
            if torch.cuda.device_count() > 1:
                self.net = nn.DataParallel(self.net)
            if torch.cuda.is_available():
                self.net.cuda()
            print('from scratch...')

        elif train_mode == 'finetune':
            self._load()
            if torch.cuda.device_count() > 1:
                self.net = nn.DataParallel(self.net,device_ids=range(torch.cuda.device_count()))
            if torch.cuda.is_available():
                self.net.cuda()
            print('finetuning...')

        elif train_mode == 'update':
            self._load()
            print('updating...')

        else:
            ValueError('train_mode is error...')


    def _load(self):
        _state_dict = torch.load(os.path.join(self.config['pretrained_path'], self.config['pretrained_file']),
                                map_location=None)
        # for multi-gpus
        state_dict = OrderedDict()
        for item, value in _state_dict.items():
            if 'module' in item.split('.')[0]:
                name = '.'.join(item.split('.')[1:])
            else:
                name = item
            state_dict[name] = value
        # for handling in case of different models compared to the saved pretrain-weight
        model_dict = self.net.state_dict()
        diff = {k: v for k, v in model_dict.items() if \
                k in state_dict and state_dict[k].size() != v.size()}
        print('diff: ', [i for i, v in diff.items()])
        state_dict.update(diff)
        self.net.load_state_dict(state_dict)
class ExampleModel(BaseModel):
    def __init__(self, config):
        super(ExampleModel, self).__init__(config)
        self.config = config
        self.interface = NetModule(self.config['model_module_name'],
                                   self.config['model_net_name'],
                                   self.config['official_source'])
        self.gpus = len(utils.gpus_str_to_list(self.config['gpu_id']))
        self.create_model()

    def create_model(self):
        self.net = self.interface.create_model(
            weights=None, include_top=True, classes=self.config['num_classes'])
        # # compile the model (should be done *after* setting layers to non-trainable)
        # # for layer in self.model.net.layers[:25]:
        # #     layer.trainable = False

        # load weights before multi-gpu-model
        self.load()
        if self.gpus > 1:
            from keras.utils import multi_gpu_model
            self.net = multi_gpu_model(self.net, gpus=self.gpus)

    def load(self):
        # train_mode: 0:from scratch, 1:finetuning, 2:update
        train_mode = self.config['train_mode']
        if train_mode == 'fromscratch':
            print('from scratch...')

        elif train_mode == 'finetune':
            self._load()
            print('finetuning...')

        elif train_mode == 'update':
            self._load_all_weights()
            print('updating...')

        else:
            ValueError('train_mode is error...')

    def _load(self):
        import h5py
        f = h5py.File(
            os.path.join(self.config['pretrained_path'],
                         self.config['pretrained_file']))
        # get weights of .h5 file
        state_dict = OrderedDict()
        for layer, g in f.items():
            for p_name in g.keys():
                param = g[p_name]
                tmp = []
                for k_name in param.keys():
                    kk = param.get(k_name)[:]
                    tmp.append(kk)
                state_dict[p_name] = tmp
        # get net tensor name
        model_tensor = self.net.layers
        tensor_list = [i.name for i in model_tensor]

        print('weights loading ...')
        # find node and weights in same name and size between .h5 file and net
        for k, v in state_dict.items():
            if k in tensor_list:
                layer_weights = self.net.get_layer(k).get_weights()
                for idx, weights in enumerate(layer_weights):
                    if v[idx].shape == weights.shape:
                        self.net.get_layer(k).set_weights(v)
        print('loaded weights done!')

    def _load_all_weights(self):
        pretrained_model_path = os.path.join(self.config['pretrained_path'],
                                             self.config['pretrained_file'])
        print('weights loading ...')
        self.net.load_weights(pretrained_model_path)
        print('loaded weights done!')