def load_pretrained_model(prefix): checkpoint = torch.load(add_prefix(prefix, 'model_best.pth.tar')) model = resnet18(is_ptrtrained=False) print('load pretrained resnet18 successfully.') model.load_state_dict(remove_prefix(checkpoint['state_dict'])) # print('best acc=%.4f' % checkpoint['best_accuracy']) return model
def make_dataset(dir, max_dataset_size=float("inf")): cache = dir.rstrip('/') + '.txt' if os.path.isfile(cache): print("Using filelist cached at %s" % cache) with open(cache) as f: images = [line.strip() for line in f] # patch up image list with new loading method if images[0].startswith(dir): print("Using image list from older version") image_list = [] for image in images: image_list.append(image) else: print("Adding prefix to saved image list") image_list = [] prefix = os.path.dirname(dir.rstrip('/')) for image in images: image_list.append(os.path.join(prefix, image)) return image_list print("Walking directory ...") images = [] assert os.path.isdir(dir), '%s is not a valid directory' % dir for root, _, fnames in sorted(os.walk(dir, followlinks=True)): for fname in fnames: if is_image_file(fname): path = os.path.join(root, fname) images.append(path) image_list = images[:min(max_dataset_size, len(images))] with open(cache, 'w') as f: prefix = os.path.dirname(dir.rstrip('/')) + '/' for i in image_list: f.write('%s\n' % util.remove_prefix(i, prefix)) return image_list
def resnet18(is_ptrtrained, path=None): classifier = ResNet(BasicBlock, [2, 2, 2, 2]) print('use custom-defined resnet18 as classifier and the last convolutional layer feature size is (4,4).') print(classifier) if is_ptrtrained: checkpoint = torch.load(path) classifier.load_state_dict(remove_prefix(checkpoint['state_dict'])) print('load pretrained classifier!') return classifier
def load_pretrained_model(pretrained_path, model_type): checkpoint = torch.load(add_prefix(pretrained_path, 'model_best.pth.tar')) if model_type == 'vgg': model = vgg19(pretrained=False, num_classes=2) print('load vgg successfully.') elif model_type == 'resnet': model = resnet18(is_ptrtrained=False) print('load resnet18 successfully.') else: raise ValueError('') model.load_state_dict(remove_prefix(checkpoint['state_dict'])) return model
def load_pretrained_model(prefix, model_type): if model_type == 'resnet': model = resnet18(is_ptrtrained=False) elif model_type == 'vgg': model = vgg19(num_classes=2, pretrained=False) else: raise ValueError('') checkpoint = torch.load(add_prefix(prefix, 'model_best.pth.tar')) print('load pretrained model successfully.') model.load_state_dict(remove_prefix(checkpoint['state_dict'])) print('best acc=%.4f' % checkpoint['best_accuracy']) return model
def __init__(self, root, loader=folder.default_loader, extensions=folder.IMG_EXTENSIONS, transform=None, target_transform=None, is_valid_file=None, return_path=False): super(ImageDataset, self).__init__(root, transform=transform, target_transform=target_transform) # self.root = root classes, class_to_idx = self._find_classes(self.root) cache = self.root.rstrip('/') + '.txt' if os.path.isfile(cache): print("Using directory list at: %s" % cache) with open(cache) as f: samples = [] for line in f: (path, idx) = line.strip().split(';') samples.append((os.path.join(self.root, path), int(idx))) else: print("Walking directory: %s" % self.root) samples = folder.make_dataset(self.root, class_to_idx, extensions, is_valid_file) with open(cache, 'w') as f: for line in samples: path, label = line f.write('%s;%d\n' % (util.remove_prefix( path, self.root).lstrip('/'), label)) if len(samples) == 0: raise (RuntimeError("Found 0 files in subfolders of: " + self.root + "\n" "Supported extensions are: " + ",".join(extensions))) self.loader = loader self.extensions = extensions self.classes = classes self.class_to_idx = class_to_idx self.samples = samples self.targets = [s[1] for s in samples] self.imgs = samples self.return_path = return_path
def load_pretrained_model(prefix): checkpoint = torch.load(add_prefix(prefix, 'model_best.pth.tar')) model = vgg19(num_classes=2, pretrained=False) print('load pretrained vgg19 successfully.') model.load_state_dict(remove_prefix(checkpoint['state_dict'])) return model