Пример #1
0
def init_model(gpu_ids, model_name):
    # model_name = 'pnasnet5large'
    # could be fbresnet152 or inceptionresnetv2
    model = pretrainedmodels.__dict__[model_name](num_classes=1000,
                                                  pretrained='imagenet')
    model.eval()
    load_img = utils.LoadImage()

    # transformations depending on the model
    # rescale, center crop, normalize, and others (ex: ToBGR, ToRange255)
    tf_img = utils.TransformImage(model)
    """
    TODO(WG): Would be nice to use something like DataParallel, but that only does forward pass on given module.
    Need to stop before logits step. 
    Should create wrapper for pretrainedmodels that does the MPI-like ops across GPUs on model.features modules:
    1) replicated
    2) scatter
    3) parallel_apply
    4) gather
    Would have to know what layers are being used on each model. 
    """
    if torch.cuda.is_available():
        model = model.cuda(device=gpu_ids[0])

    return load_img, tf_img, model
Пример #2
0
 def __init__(self, cnn_model, image_dir, index_file, caption_file, vocab):
     self.load_img = utils.LoadImage()
     self.tf_img = utils.TransformImage(cnn_model)
     self.image_dir = image_dir
     self.image_indices = codecs.open(index_file,
                                      encoding="utf-8").readlines()
     self.captions = codecs.open(caption_file, encoding="utf-8").readlines()
     self.vocab = vocab
     self.vocab_keys = vocab.keys()
     self.max_len = 50
Пример #3
0
 def __init__(self, img_paths, model, img_size=224, augment=False):
     self.load_img = utils.LoadImage()
     additional_args = {}
     if augment:
         additional_args = {
             'random_crop': True, 'random_hflip': False,
             'random_vflip': False
         }
     self.tf_img = utils.TransformImage(
         model, scale=img_size / 256, **additional_args)
     self.img_paths = img_paths
Пример #4
0
 def __init__(self,
              root,
              datalist,
              transform=None,
              target_transform=None,
              space='RGB'):
     self.root = root
     self.datalist = datalist
     self.transform = transform
     self.target_transform = target_transform
     self.loader = putils.LoadImage(space)
Пример #5
0
 def __init__(self,
              data_dir,
              train=True,
              transform=None,
              tta_transform=None,
              tta_times=1):
     self.data_dir = data_dir
     self.coco = COCO(os.path.join(data_dir, 'metadata_utf8.json'))
     self.load_img = utils.LoadImage()
     self.transform = transform
     self.train = train
     self.tta_times = tta_times
     self.tta_transform = tta_transform
     self._prepare_mappings()
Пример #6
0
    def __getitem__(self, idx):
        if idx < len(self.neg_list):
            image_path = self.neg_list[idx]
            label = np.array(0).reshape([1, 1])
        else:
            image_path = self.pos_list[idx - len(self.neg_list)]
            label = np.array(1).reshape([1, 1])

        input_image = utils.LoadImage()(image_path)
        # hot_label = np.zeros(2, dtype=np.int32)
        # hot_label[label] = 1
        if self.transform:
            input_image = self.transform(input_image)

        return input_image, label.astype(np.long)
Пример #7
0
def test_multi_process_infer():
    model = pretrainedmodels.__dict__["resnet18"](num_classes=1000,
                                                  pretrained='imagenet')
    tf_img = utils.TransformImage(model)
    load_img = utils.LoadImage()
    img_list = [
        "010.jpg", "004.jpg", "005.jpg", "011.jpg", "012.jpg", "boy.jpg"
    ]
    res = {}
    t0 = time.time()
    p_list = []
    my_queue = Queue()
    with Manager() as manager:
        pred_list = manager.list()
        my_pool = Pool(4)
        for img in img_list:
            input_img = load_img(img)
            input_tensor = tf_img(
                input_img)  # 3x400x225 -> 3x299x299 size may differ
            input_tensor = input_tensor.unsqueeze(
                0)  # 3x299x299 -> 1x3x299x299
            input_img = torch.autograd.Variable(input_tensor,
                                                requires_grad=False)
            print("image:", img)
            p = Process(target=infer_process,
                        args=(my_queue, input_img, model))
            p.start()
            p_list.append(p)
            # p.start()
            # p.join()
            # xx = my_pool.apply_async(infer_process,args=(pred_list,input_img,model,),callback=infer_callback)
            # res[img] = xx
        # my_pool.close()
        # my_pool.join()
    for p in p_list:
        p.join()

    while not my_queue.empty():
        value = my_queue.get(True)
        print("queue get a predict result:", value)
        # time.sleep(random.random())

    print("Time cost:", time.time() - t0)
    print("模型推理完毕...")
Пример #8
0
def main():
    global args
    args = parser.parse_args()

    for arch in args.arch:
        # Load Model
        model = pretrainedmodels.__dict__[arch](num_classes=1000,
                                                pretrained='imagenet')
        model.eval()

        path_img = args.path_img
        # Load and Transform one input image
        load_img = utils.LoadImage()
        tf_img = utils.TransformImage(model)

        input_data = load_img(args.path_img)  # 3x400x225
        input_data = tf_img(input_data)  # 3x299x299
        input_data = input_data.unsqueeze(0)  # 1x3x299x299
        input = torch.autograd.Variable(input_data)

        # Load Imagenet Synsets
        with open('data/imagenet_synsets.txt', 'r') as f:
            synsets = f.readlines()

        # len(synsets)==1001
        # sysnets[0] == background
        synsets = [x.strip() for x in synsets]
        splits = [line.split(' ') for line in synsets]
        key_to_classname = {spl[0]: ' '.join(spl[1:]) for spl in splits}

        with open('data/imagenet_classes.txt', 'r') as f:
            class_id_to_key = f.readlines()

        class_id_to_key = [x.strip() for x in class_id_to_key]

        # Make predictions
        output = model(input)  # size(1, 1000)
        max, argmax = output.data.squeeze().max(0)
        class_id = argmax[0]
        class_key = class_id_to_key[class_id]
        classname = key_to_classname[class_key]

        print("'{}': '{}' is a '{}'".format(arch, path_img, classname))
Пример #9
0
def test_batch_infer():
    model = pretrainedmodels.__dict__["resnet18"](num_classes=1000,
                                                  pretrained='imagenet')
    tf_img = utils.TransformImage(model)
    load_img = utils.LoadImage()
    img_list = [
        "010.jpg", "004.jpg", "005.jpg", "011.jpg", "012.jpg", "boy.jpg"
    ]
    t0 = time.time()
    for img in img_list:
        input_img = load_img(img)
        input_tensor = tf_img(
            input_img)  # 3x400x225 -> 3x299x299 size may differ
        input_tensor = input_tensor.unsqueeze(0)  # 3x299x299 -> 1x3x299x299
        input_img = torch.autograd.Variable(input_tensor, requires_grad=False)
        output = model(input_img)
        _, predicted = torch.max(output.data, 1)
        print("model predict result is:", predicted.numpy()[0])
    print("Time cost:", time.time() - t0)
Пример #10
0
def init():
    global model
    seed = 0
    torch.manual_seed(seed)
    logging.basicConfig(level=logging.DEBUG)
    model_name = 'nasnetalarge'
    model = pretrainedmodels.__dict__[model_name](num_classes=1000,
                                                  pretrained='')
    model_path = Model.get_model_path('full_chexray_e17')

    # if you want to test the run script on your local system you can uncomment the following line and comment the one above
    #model_path = os.path.join(base_path,'output/chexray_nasnet_e10.pth.tar')
    n_classes = 14

    in_ftrs = model.last_linear.in_features
    model.last_linear = nn.Sequential(nn.Linear(in_ftrs, 256), nn.ReLU(),
                                      nn.Dropout(p=0.4),
                                      nn.Linear(256, n_classes), nn.Sigmoid())

    chkpt = torch.load(model_path, map_location=lambda storage, loc: storage)
    state_dict = chkpt['state_dict']

    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k[7:]  # remove 'module.' of dataparallel
        new_state_dict[name] = v

    model.load_state_dict(new_state_dict)
    global load_img
    load_img = utils.LoadImage()
    global credentials
    credentials = {
        "datastore_name":
        "chexrayds",
        "container_name":
        "chexray-images",
        "account_name":
        "chexray14",
        "account_key":
        "rXogWjkm8lXV0QfHAety1KktKGxbb1pMGoWkd4E9FI3QN+MW2qp4bWlFRymGS2zR0MyroV+VJcgHurr8+dhQdg=="
    }
    model.eval()
Пример #11
0
def createFeatures3(encoder, img_dir, lbl_train, lbl_test):
    import pretrainedmodels.utils as utils
    load_img = utils.LoadImage()
    tf_img = utils.TransformImage(encoder)

    train_dir = '%s/natural-image_training' % img_dir
    featuresTrain = []

    for i in range(len(lbl_train)):
        file = '%s/%s' % (train_dir, lbl_train[i])
        sample = transformFile2(load_img, tf_img, file)
        featuresTrain.append(encoder(sample))

    test_dir = '%s/natural-image_test' % img_dir
    featuresTest = []
    for i in range(len(lbl_test)):
        file = '%s/%s' % (test_dir, lbl_test[i])
        sample = transformFile2(load_img, tf_img, file)
        featuresTest.append(encoder(sample))
    return featuresTrain, featuresTest
Пример #12
0
def extract():
    cuda_is_availabe = torch.cuda.is_available()
    print('CUDA is available' if cuda_is_availabe else 'CUDA is NOT available')
    xception_model = xception_with_pooling_features(num_classes=1000,
                                                    pretrained='imagenet')
    if cuda_is_availabe:
        xception_model = xception_model.to(torch.device('cuda:0'))
    photo_df = pd.read_json('photos/photo.json', lines=True)

    load_img = utils.LoadImage()

    # transformations depending on the model
    # rescale, center crop, normalize, and others (ex: ToBGR, ToRange255)
    tf_img = utils.TransformImage(xception_model)

    photo_df['features'] = None
    # Extract features just for a small subset.
    #photo_df = photo_df.loc[:100-1,:]
    for index, row in tqdm(photo_df.iterrows(),
                           total=photo_df.shape[0],
                           desc="Extracting features from photos"):
        photo_id = row['photo_id']
        file_name = os.path.join('photos', 'photos', photo_id + '.jpg')
        input_img = load_img(file_name)
        input_tensor = tf_img(input_img)  # 3x?x? -> 3x299x299 size may differ
        input_tensor = input_tensor.unsqueeze(0)  # 3x299x299 -> 1x3x299x299
        if cuda_is_availabe:
            input_tensor = input_tensor.cuda()
        with torch.no_grad():
            #input = torch.autograd.Variable(input_tensor, requires_grad=False)
            output_features = xception_model.features(
                input_tensor).cpu().numpy()  # 1x2048x1x1
        output_features = np.reshape(output_features, (-1, ))
        row['features'] = output_features

    os.makedirs('.cache', exist_ok=True)
    photo_df.to_pickle('.cache/photo.pkl')
Пример #13
0
import torch
import torch.nn as nn
from torch.autograd import Variable
import pretrainedmodels as pm
import pretrainedmodels.utils as utils

# torch 1.0.x
set_grad_enabled = getattr(torch.autograd, 'set_grad_enabled', None)

pm_args = []
for model_name in pm.model_names:
    for pretrained in pm.pretrained_settings[model_name]:
        if pretrained in ['imagenet', 'imagenet+5k']:
            pm_args.append((model_name, pretrained))
#여기를 건드려야 할 것 같은데...?!
img = utils.LoadImage()('data/cat.jpg')


def equal(x, y):
    return torch.all(torch.lt(torch.abs(torch.add(x, -y)), 1e-12))


@pytest.mark.parametrize('model_name, pretrained', pm_args)
def test_pm_imagenet(model_name, pretrained):
    if set_grad_enabled: set_grad_enabled(False)

    print('test_pm_imagenet("{}")'.format(model_name))
    net = pm.__dict__[model_name](num_classes=1000, pretrained=pretrained)
    net.eval()

    tensor = utils.TransformImage(net)(img)
Пример #14
0
        probabilities_all.append(probabilities)
        
        if (j+1)%2 == 0:
            
            input_file.write("fitness at iteration " + str(j+1) + ' ' + str(result[1]))
            input_file.write("\n")
            input_file.write("Target probability: " +str(target_prob) + ":: Original probability: " +str(orig_prob))
            input_file.write("\n")
            input_file.write('Top scorer: '+ str(labels[int(np.argmax(probabilities))]) +
                   ', probability:: ' + str(np.max(probabilities)))
            input_file.write("\n")

    return j,history, probabilities_all, orig_probs, target_probs


load_img = utils.LoadImage()
tf_img = utils.TransformImage(model)

NUMBER_OF_LINE_PERMUTATIONS = 10

import random
for iterations in range(NUMBER_OF_FILES):
    target_class = random.randint(0,1000)    
    for permutation in range(NUMBER_OF_LINE_PERMUTATIONS):

        xpoint = random.randint(0,224)
        ypoint = random.randint(0,224)
        xpoint2 = random.randint(0,224)
        ypoint2 = random.randint(0,224)
    #     xpoint,ypoint,xpoint2,ypoint2 = 13,5,200,210
        print ('Random points: ', xpoint,ypoint,xpoint2,ypoint2)
 def __init__(self, cnn_model, image_dir, index_file):
     self.load_img = utils.LoadImage()
     self.tf_img = utils.TransformImage(cnn_model)
     self.image_dir = image_dir
     self.image_indices = codecs.open(index_file,
                                      encoding="utf-8").readlines()
Пример #16
0
def load_model():
    if args.architect.startswith('resnet'):
        if args.architect == 'resnet18':
            model = models.resnet18(pretrained=True)
        if args.architect == 'resnet50':
            model = models.resnet50(pretrained=True)
        model.avgpool = nn.AdaptiveAvgPool2d(1)
        if args.nodoublefc is True:
            model.fc = nn.Linear(in_features=model.fc.in_features,
                                 out_features=37)
        else:
            model.fc = nn.Sequential(
                nn.Linear(in_features=model.fc.in_features, out_features=1024),
                nn.ReLU(), nn.Linear(in_features=1024,
                                     out_features=37))  # change dis shit
    elif args.architect == 'squeezenet':
        model = models.squeezenet1_1(pretrained=True)
        model.classifier = nn.Sequential([
            nn.Dropout(p=0.5),
            nn.Conv2d(512, 37, kernel_size=(1, 1), stride=(1, 1)),
            nn.ReLU(inplace=True),
            nn.AdaptiveAvgPool2d(1)
        ])
    elif args.architect == 'mobilenet':
        model = mobilenetv2()
        model.load_state_dict(
            torch.load('./mobilenetv2/pretrained/mobilenetv2-36f4e720.pth'))
        model.avgpool = nn.AdaptiveAvgPool2d(1)
        if args.nodoublefc is True:
            model.classifier = nn.Linear(
                in_features=model.classifier.in_features, out_features=37)
        else:
            model.classifier = nn.Sequential(
                nn.Linear(in_features=model.classifier.in_features,
                          out_features=1024), nn.ReLU(),
                nn.Linear(in_features=1024,
                          out_features=37))  # change dis shit
    elif args.architect in pretrainedmodels.__dict__:  # no blabla fully conv for these models
        model = pretrainedmodels.__dict__[args.architect](
            num_classes=1000, pretrained='imagenet')
        load_img = utils.LoadImage()
        tf_img = utils.TransformImage(model)
    else:
        raise 'no known architecture %s' % args.architect
    filename = '%s.checkpoint.pth.tar' % (args.tag)
    if args.start_tag is not None:
        start_filename = '%s.checkpoint.pth.tar' % (args.start_tag)
    else:
        start_filename = filename

    checkpoint = None
    if os.path.exists(start_filename) and args.resume:
        checkpoint = torch.load(start_filename)
        model.load_state_dict(checkpoint['state_dict'])
        print 'loaded weights from file ', start_filename
    for i, child in enumerate(model.children()):
        print child
        if i < args.freeze:
            for param in child.parameters():
                param.requires_grad = False
    return model.cuda(), checkpoint, filename
Пример #17
0
import dlib
import numpy as np
from sklearn.cluster import KMeans
from sklearn.cluster import MeanShift, DBSCAN
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import pickle
import tkinter
from PIL import ImageTk
from PIL import Image
import cv2
from imdirect import imdirect_open
from sklearn.preprocessing import Normalizer
import random

load_img = utils_pretrained.LoadImage()


def _is_valid_img(img_path):
    try:
        load_img(img_path)
        return True
    except Exception:
        return False


def filter_invalid_images(img_paths, num_workers=4, progress=False):
    """Filter invalid images before computing expensive features."""
    with multiprocessing.Pool(num_workers) as p:
        if progress:
            load_works = list(
Пример #18
0
import pretrainedmodels.utils as pretrained_utils

from models import Encoder


print(pretrainedmodels.model_names)
test_models = ['pnasnet5large', 'nasnetalarge', 'senet154', 'polynet', 'inceptionv4', 'xception', 'resnet152']
attr = {model_name:{} for model_name in test_models}


for model_name in test_models:
    model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')
    model.cuda()
    model.eval()
    with torch.no_grad():
        load_img = pretrained_utils.LoadImage()
        tf_img = pretrained_utils.TransformImage(model)
        path_img = '../test/2.png'
        input_img = load_img(path_img)
        input_tensor = tf_img(input_img)
        input_tensor = input_tensor.unsqueeze(0).cuda()
        time_used_per_model = []
        for i in range(100):
            s = time.time()
            output_features  = model.features(input_tensor)
            e = time.time()
            time_used_per_model.append(e-s)
            print(e-s, model_name)
        attr[model_name]['time_used'] = time_used_per_model
        attr[model_name]['size'] = input_tensor.size()
        x = x.view(x.size(0), -1)
        x = self.dropout(x)
        x = self.last_linear(x)
        return x


features_model = pretrainedmodels.__dict__['nasnetamobile'](num_classes=1000, pretrained='imagenet')
features_model.eval()
features_model.cuda()

classifier_model = Classifier()
classifier_model.load_state_dict(torch.load('scent_model.pt'))
classifier_model.eval()
classifier_model.cuda()

load_image = utils.LoadImage()
transform_image = utils.TransformImage(features_model)

class_labels = "citrus	floral	fruity	woody	oriental	musk	aromatic	water	mossy	green".split(
    "\t")

image = load_image('test/Screen Shot 2018-03-13 at 7.56.47 PM.png')
image = transform_image(image)

image = torch.autograd.Variable(image.unsqueeze(0).cuda())

outputs = classifier_model(features_model.features(image))
for predicted_label in outputs:
    label_probs, label_indices = torch.topk(predicted_label, 3)
    label_probs = label_probs.cpu().data.numpy()
    label_indices = label_indices.cpu().data.numpy()
Пример #20
0
def eval(dataset, models, batch_size):
    dataset_filename = dataset
    if models == 'all':
        models = all_models
    else:
        models = models.split(',')
    for model in models:
        assert model in all_models

    dataset_filepath = pathlib.Path(__file__).parent / '../data/datasets' / (
        dataset_filename + '.json')
    print('Reading dataset from {} ...'.format(dataset_filepath))
    with open(dataset_filepath, 'r') as f:
        dataset = json.load(f)
    cur_imgs = [x[0] for x in dataset['image_filenames']]

    imgnet = imagenet.ImageNetData()
    cds = candidate_data.CandidateData(load_metadata_from_s3=False,
                                       exclude_blacklisted_candidates=False)
    loader = image_loader.ImageLoader(imgnet, cds)

    pbar = tqdm(total=len(cur_imgs), desc='Dataset download')
    img_data = loader.load_image_bytes_batch(
        cur_imgs,
        size='scaled_500',
        verbose=False,
        download_callback=lambda x: pbar.update(x))
    pbar.close()

    for model in tqdm(models, desc='Model evaluations'):
        if (model not in extra_models):
            tqdm.write('Evaluating {}'.format(model))
            resize_size = 256
            center_crop_size = 224
            if model == 'inception_v3':
                resize_size = 299
                center_crop_size = 299
            data_loader = eval_utils.get_data_loader(
                cur_imgs,
                imgnet,
                cds,
                image_size='scaled_500',
                resize_size=resize_size,
                center_crop_size=center_crop_size,
                batch_size=batch_size)
            pt_model = getattr(torchvision.models, model)(pretrained=True)
            if (torch.cuda.is_available()):
                pt_model = pt_model.cuda()
            pt_model.eval()
            tqdm.write('    Number of trainable parameters: {}'.format(
                sum(p.numel() for p in pt_model.parameters()
                    if p.requires_grad)))

            predictions, top1_acc, top5_acc, total_time, num_images = eval_utils.evaluate_model(
                pt_model, data_loader, show_progress_bar=True)
            tqdm.write('    Evaluated {} images'.format(num_images))
            tqdm.write('    Top-1 accuracy: {:.2f}'.format(100.0 * top1_acc))
            tqdm.write('    Top-5 accuracy: {:.2f}'.format(100.0 * top5_acc))
            tqdm.write(
                '    Total time: {:.1f}  (average time per image: {:.2f} ms)'.
                format(total_time, 1000.0 * total_time / num_images))
            npy_out_filepath = pathlib.Path(
                __file__).parent / '../data/predictions' / dataset_filename / (
                    model + '.npy')
            npy_out_filepath = npy_out_filepath.resolve()
            directory = os.path.dirname(npy_out_filepath)
            if not os.path.exists(directory):
                os.makedirs(directory)
            if (os.path.exists(npy_out_filepath)):
                old_preds = np.load(npy_out_filepath)
                np.save(f'{npy_out_filepath}.{int(time.time())}', old_preds)
                print('checking old preds is same as new preds')
                if not np.allclose(old_preds, predictions):
                    diffs = np.round(old_preds - predictions, 4)
                    print('old preds != new preds')
                else:
                    print('old preds == new_preds!')
            np.save(npy_out_filepath, predictions)
            tqdm.write('    Saved predictions to {}'.format(npy_out_filepath))
        else:
            tqdm.write('Evaluating extra model {}'.format(model))
            if (model in {"dpn68b", "dpn92", "dpn107"}):
                pt_model = pretrainedmodels.__dict__[model](
                    num_classes=1000, pretrained='imagenet+5k')
            else:
                pt_model = pretrainedmodels.__dict__[model](
                    num_classes=1000, pretrained='imagenet')
            tf_img = pretrained_utils.TransformImage(pt_model)
            load_img = pretrained_utils.LoadImage()
            tqdm.write('    Number of trainable parameters: {}'.format(
                sum(p.numel() for p in pt_model.parameters()
                    if p.requires_grad)))

            #print(pt_model)
            #print(load_img)
            dataset = eval_utils.ImageLoaderDataset(cur_imgs,
                                                    imgnet,
                                                    cds,
                                                    'scaled_500',
                                                    transform=tf_img)

            data_loader = torch.utils.data.DataLoader(dataset,
                                                      batch_size=batch_size,
                                                      shuffle=False,
                                                      num_workers=0,
                                                      pin_memory=True)
            if (torch.cuda.is_available()):
                pt_model = pt_model.cuda()

            pt_model.eval()
            predictions, top1_acc, top5_acc, total_time, num_images = eval_utils.evaluate_model(
                pt_model, data_loader, show_progress_bar=True)
            tqdm.write('    Evaluated {} images'.format(num_images))
            tqdm.write('    Top-1 accuracy: {:.2f}'.format(100.0 * top1_acc))
            tqdm.write('    Top-5 accuracy: {:.2f}'.format(100.0 * top5_acc))
            tqdm.write(
                '    Total time: {:.1f}  (average time per image: {:.2f} ms)'.
                format(total_time, 1000.0 * total_time / num_images))
            npy_out_filepath = pathlib.Path(
                __file__).parent / '../data/predictions' / dataset_filename / (
                    model + '.npy')
            npy_out_filepath = npy_out_filepath.resolve()
            directory = os.path.dirname(npy_out_filepath)
            if not os.path.exists(directory):
                os.makedirs(directory)
            if (os.path.exists(npy_out_filepath)):
                old_preds = np.load(npy_out_filepath)
                np.save(f'{npy_out_filepath}.{int(time.time())}', old_preds)
                print('checking old preds is same as new preds')
                if not np.allclose(old_preds, predictions):
                    diffs = np.round(old_preds - predictions, 4)
                    print('old preds != new preds')
                else:
                    print('old preds == new_preds!')
            np.save(npy_out_filepath, predictions)
            tqdm.write('    Saved predictions to {}'.format(npy_out_filepath))