# train.py

# Import detecto libs, the lib is great and does all the work
# https://github.com/alankbi/detecto
from detecto import core
from detecto.core import Model

# Load all images and XML files from the Classification section
dataset = core.Dataset('images_classified/')

# We initalize the Model and map it to the label we used in labelImg classification
model = Model(['aboriginal_flag'])

# The model.fit() method is the bulk of this program
# It starts training your model synchronously (the lib doesn't expose many logs)
# It will take up quite a lot of resources, and if it crashes on your computer
# you will probably have to rent a bigger box for a few hours to get this to run on.
# Epochs essentially means iterations, the more the merrier (accuracy) (up to a limit)
# It will take quite a while for this process to end, grab a wine.
model.fit(dataset, epochs=10, verbose=True)

# TIP: The more images you classify and the more epochs you run, the better your results will be.

# Once the model training has finished, we can save to a single file.
# Passs this file around to anywhere you want to now use your newly trained model.
model.save('model.pth')

# If you have got this far, you've already trained your very own unique machine learning model
# What are you going to do with this new found power?
示例#2
0
# cv2.imshow('img',image)
# cv2.waitKey(0)
img = cv2.imread(base_path+sample_image)
# image = read_image(base_path+sample_image)
# plt.imshow(image)
# plt.show()
# cv2.imshow('img',img)
# cv2.waitKey(0)

dataset = Dataset(base_path)
img, targets = dataset[10]
show_labeled_image(img, targets['boxes'], targets['labels'])

labels = ['spiderman', 'venom']
model = Model(labels)
model.fit(dataset)
torch.save(model, 'model.pth')

# directory = r'C:\Users\kapsi\Desktop\WdPO_P\test'
# file_count = sum(len(files) for _, _, files in os.walk(directory))
# if file_count == 0:
#    print('Theres nothing to show! Closing...')
#
# else:
#    pics = list()
#    for i in range(file_count):
#       path = test_data + '{}.jpg'.format(i)
#       img_test = cv2.imread(path)
#       img_test = cv2.cvtColor(img_test, cv2.COLOR_BGR2RGB)
#       pics.append(img_test)
#       i += 1
示例#3
0
from detecto.core import Model
from detecto import utils, visualize
from detecto.core import Dataset

# If your images and labels are in separate folders
dataset = Dataset('/Users/mahesh/mltrain/')

from detecto.visualize import show_labeled_image

image, targets = dataset[0]
#show_labeled_image(image, targets['boxes'], targets['labels'])

your_labels = ['malu']
model = Model(your_labels)

model.fit(dataset, verbose=True)
示例#4
0
import matplotlib.pyplot as plt
from torchvision import transforms
from detecto.utils import normalize_transform
from detecto.core import Dataset, DataLoader, Model

IMAGE_DIR = '/Users/noahmushkin/codes/selenium-python-scraping/data/images/cameras/'
LABEL_DIR = '/Users/noahmushkin/codes/selenium-python-scraping/data/labeled_cams_convert/'

img_transform = transforms.Compose([
    transforms.ToPILImage(),
    # Note: all images with a size smaller than 800 will be scaled up in size
    transforms.Resize(400),
    transforms.RandomHorizontalFlip(0.5),
    transforms.ColorJitter(saturation=0.2),
    transforms.ToTensor(),  # required
    normalize_transform(),  # required
])

dataset = Dataset(LABEL_DIR, IMAGE_DIR, transform=img_transform)
labels = ['camera']
model = Model(classes=labels)
loader = DataLoader(dataset, batch_size=32, shuffle=True)
losses = model.fit(loader, epochs=10, learning_rate=0.005)
plt.plot(losses)
plt.show()
model.save('cam_model.pth')
示例#5
0
Training script for Py-Torch based Convolutional Neural Network Object Detection of radar data

@author: Yash Sarda
"""

from detecto.core import Model, Dataset
import matplotlib
import matplotlib.pyplot as plt

########################################################

tdataset = Dataset('training/2500/train/')  # Training dataset
vdataset = Dataset('training/2500/test/')  # Evaluation dataseet

model = Model(['fall'])

# Keep the learning rate low, otherwise the loss will be too high
loss = model.fit(tdataset,
                 vdataset,
                 epochs=15,
                 learning_rate=0.001,
                 gamma=0.2,
                 lr_step_size=5,
                 verbose=True)

plt.plot(loss)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
model.save('RASRmodl.pth')