Esempio n. 1
0
def work():
    train_trans = transforms.Compose(
        [transforms.RandomResizedCrop(224),
         transforms.ToTensor()])
    # https://gluon-cv.mxnet.io/build/examples_datasets/imagenet.html
    # You need to specify ``root`` for ImageNet if you extracted the images into
    # a different folder, not use rec but general images, such as  *.jpg
    train_data = DataLoader(ImageNet(
        train=True, root="/home1/ImageNet_ILSVRC2012/ILSVRC2012_img_train/").
                            transform_first(train_trans),
                            batch_size=128,
                            shuffle=True)

    for x, y in train_data:
        print(x.shape, y.shape)
        break
    from gluoncv.utils import viz

    val_dataset = ImageNet(train=False)
    viz.plot_image(val_dataset[1234][0])  # index 0 is image, 1 is label
    viz.plot_image(val_dataset[4567][0])
Esempio n. 2
0
from tf2cv.model_provider import get_model as tf2cv_get_model
import tensorflow as tf
from gluoncv.data import ImageNet
import numpy as np
from NN.img2tensor import convert_image

net = tf2cv_get_model("AlexNet", pretrained=True, data_format="channels_last")
x = convert_image("img_23.jpg")
x = ((x - np.array((0.485, 0.456, 0.406))) / np.array(
    (0.229, 0.224, 0.225))).reshape(1, 224, 224, 3)
y = net(x)
probs = tf.nn.softmax(y)

top_k = 5
probs_np = probs.numpy().squeeze(axis=0)
top_k_inds = probs_np.argsort()[::-1][:top_k]
classes = ImageNet().classes
print("The input picture is classified to be:")
for k in range(top_k):
    print("{idx}: [{class_name}], with probability {prob:.3f}.".format(
        idx=(k + 1),
        class_name=classes[top_k_inds[k]],
        prob=probs_np[top_k_inds[k]]))
Esempio n. 3
0
"""


from gluoncv.data import ImageNet
from mxnet.gluon.data import DataLoader
from mxnet.gluon.data.vision import transforms

train_trans = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.ToTensor()
])

# You need to specify ``root`` for ImageNet if you extracted the images into
# a different folder
train_data = DataLoader(
    ImageNet(train=True).transform_first(train_trans),
    batch_size=128, shuffle=True)

#########################################################################
for x, y in train_data:
    print(x.shape, y.shape)
    break


#########################################################################
# Plot some validation images
from gluoncv.utils import viz
val_dataset = ImageNet(train=False)
viz.plot_image(val_dataset[1234][0])  # index 0 is image, 1 is label
viz.plot_image(val_dataset[4567][0])
Esempio n. 4
0
from gluoncv.data import ImageNet
from mxnet.gluon.data import DataLoader
from mxnet.gluon.data.vision import transforms

test_trans = transforms.Compose(
    [transforms.RandomResizedCrop(224),
     transforms.ToTensor()])

test_data = DataLoader(ImageNet(root='.',
                                train=False).transform_first(test_trans),
                       batch_size=128,
                       shuffle=True)
Esempio n. 5
0
 def get_data_loader(data_dir):
     train_dataset = ImageNet(data_dir, train=True)
     val_dataset = ImageNet(data_dir, train=False)
     return train_dataset, val_dataset