コード例 #1
0
# slic
labels = segmentation.slic(im,
                           compactness=args.compactness,
                           n_segments=args.num_superpixels)
labels = labels.reshape(im.shape[0] * im.shape[1])
u_labels = np.unique(labels)
l_inds = []
for i in range(len(u_labels)):
    l_inds.append(np.where(labels == u_labels[i])[0])

# train
from model import MyNet

model = MyNet(data.size(1), args.nChannel, args.nConv)
if use_cuda:
    model.cuda()
model.train()
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9)
label_colours = np.random.randint(255, size=(100, 3))
for batch_idx in range(args.maxIter):
    # forwarding
    optimizer.zero_grad()
    output = model(data)[0]
    output = output.permute(1, 2, 0).contiguous().view(-1, args.nChannel)
    ignore, target = torch.max(output, 1)
    im_target = target.data.cpu().numpy()
    nLabels = len(np.unique(im_target))
    if args.visualize:
        im_target_rgb = np.array([label_colours[c % 100] for c in im_target])
        im_target_rgb = im_target_rgb.reshape(im.shape).astype(np.uint8)
コード例 #2
0
    'Dot': 'dot',
    'Miss_print': 'miss_print',
    'Over_ink': 'over_ink',
    'Set_off': 'set_off',
    'Under_ink': 'under_ink',
    'Wiping': 'wiping',
}

class_names = ['dot', 'miss_print', 'over_ink', 'set_off', 'under_ink', 'wiping']

# instantiate the CNN
use_cuda = torch.cuda.is_available()
layer_sizes = [512, 256, 128]
model = MyNet(output_size=6, layer_sizes=layer_sizes)
if use_cuda:
    model = model.cuda()

model.load_state_dict(torch.load('model/model_resnet101_512_256_128_back.pt', map_location=torch.device('cpu')))


# ### Get the directory path from the argument

if __name__ == "__main__":
    dir_path = sys.argv[1].strip()
#in put the directory path
#dir_path = input('Enter the images directory path: ').strip()

#print(dir_path)
#delete the output folder if exists
for files in os.listdir(dir_path):
    if files == 'output':