Beispiel #1
0
def search():

    btn['state'] = NORMAL  #open image button is returened to enabled

    fracturedataset = core.Dataset(
        'trainingimagesgui/'
    )  #initializes the dataset the parameter is the folder in which to get the images for the dataset

    mymodel = core.Model([
        'crack'
    ])  #calls the neural network and it is looking for boxes labelled crack
    print("model imported"
          )  #print function to see if the neural network has been called

    mymodel.fit(fracturedataset)  #train the neuralnetwork on the dataset
    print("finished training custom dataset"
          )  #prints to show that the neural network has finished training
    #testing the neural network
    testimage = utils.read_image(
        filename
    )  #read the varible testimage which has the location of what is stored in variable filename
    print(filename)  #test to see the correct location is being pulled
    endresult = mymodel.predict(
        testimage)  #run the test image on the now trained neural network
    print(
        "prediction complete"
    )  #test to see that the test image has successfully ran on the neural network
    labels, boxes, scores = endresult  #all the parameters the test image will have
    print(labels)  #test to see if the correct label is printed
    print(boxes)  #this will print the coordiantes of the boxes
    print(scores)  #prints a score of how confident the neural network is
    visualize.show_labeled_image(testimage, boxes,
                                 labels)  #display the image on the screen
def train(model_name, class_names, load_model_nr, epochs, callback=None):
    train_dataset = core.Dataset(op.join(dataset_path, model_name, 'train'))
    val_dataset = core.Dataset(op.join(dataset_path, model_name, 'validate'))
    save_dir = op.join(result_dir, model_name)

    print("Training set: {}".format(len(train_dataset)))
    print("Validation set: {}".format(len(val_dataset)))

    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    model = core.Model.load(
        op.join(save_dir, 'model_{}.pth'.format(load_model_nr)), class_names)
    model.fit(train_dataset,
              val_dataset,
              save_path=save_dir,
              start_epoch=load_model_nr,
              epochs=load_model_nr + epochs,
              verbose=True,
              callback=callback)
    return model
from detecto import core, utils, visualize
from torchvision import transforms

augmentations = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomHorizontalFlip(0.5),
    transforms.ColorJitter(saturation=0.5),
    transforms.ToTensor(),
    utils.normalize_transform(),
])

val_dataset = core.Dataset('/dataset/validation_images/')
dataset = core.Dataset('//dataset/train_images/', transform=augmentations)
model = core.Model(['rust'])
loader = core.DataLoader(dataset, batch_size=2, shuffle=True)

losses = model.fit(loader,
                   val_dataset,
                   epochs=10,
                   learning_rate=0.001,
                   lr_step_size=5,
                   verbose=True)

model.save('model/model_weights.pth')
# 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?
Beispiel #5
0
    def trainMountingConfigClassifier(self,
                                      train_path,
                                      val_path,
                                      device=torch.device('cuda')):
        """
        This function uses Faster R-CNN ResNet50 FPN as the base network
        and as a transfer learning framework to train a model that performs
        object detection on the mounting configuration of solar arrays. It
        uses the training data to locate and classify mounting configuration
        of the solar installation. It uses the validation data to prevent
        overfitting and to test the prediction on the fly.

        Parameters
        -----------
        train_path: string
            This is the path to the folder that contains the training images
            Note that the directory must be structured in this format:
                    train_path/
                        ...images/
                            ......a_image_1.png
                            ......a_image_2.png
                        ...annotations/
                            ......b_image_1.xml
                            ......b_image_2.xml
        val_path: string
            This is the path to the folder that contains the validation images
            Note that the directory must be structured in this format:
                    val_path/
                        ...images/
                            ......a_image_1.png
                            ......a_image_2.png
                        ...annotations/
                            ......b_image_1.xml
                            ......b_image_2.xml
        device: string
            This argument is passed to the Model() class in Detecto.
            It determines how to run the model: either on GPU via Cuda
            (default setting), or on CPU. Please note that running the
            model on GPU results in significantly faster training times.

        Returns
        -----------
        model: detecto.core.Model object
            The final trained mounting configuration object detection
            model.
        """
        # Convert the data set combinations (png + xml) to a CSV record.
        val_labels_path = (val_path + '/annotations.csv')
        train_labels_path = (train_path + '/annotations.csv')
        utils.xml_to_csv(train_path + '/annotations/', train_labels_path)
        utils.xml_to_csv(val_path + '/annotations/', val_labels_path)
        # Custom oversampling to balance out our classes
        train_data = pd.read_csv(train_labels_path)
        class_count = pd.Series(train_data['class'].value_counts())
        train_data_resampled = train_data.copy()
        for index, count in class_count.iteritems():
            number_times_resample = class_count.max() - count
            # Randomly sample a class X times
            class_index_list = list(
                train_data[train_data['class'] == index].index)
            # Resample the list with with replacement
            idx_to_duplicate = choices(class_index_list,
                                       k=number_times_resample)
            for idx in idx_to_duplicate:
                dup = train_data.loc[idx]
                # Add to the dataframe
                train_data_resampled = \
                    train_data_resampled.append(dup, ignore_index=True)
        # Reindex after all of the duplicates have been added
        train_data_resampled = train_data_resampled.reset_index(drop=True)
        # Re-write the resampled data set
        train_data_resampled.to_csv(train_labels_path, index=False)
        custom_transforms = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize(800),
            transforms.ToTensor(),
            utils.normalize_transform()
        ])
        # Load in the training and validation data sets
        dataset = core.Dataset(train_labels_path,
                               train_path + '/images',
                               transform=custom_transforms)
        val_dataset = core.Dataset(val_labels_path, val_path + '/images')
        # Customize training options
        loader = core.DataLoader(dataset,
                                 batch_size=self.batch_size,
                                 shuffle=True)
        model = core.Model([
            "ground-fixed", "carport-fixed", "rooftop-fixed",
            "ground-single_axis_tracker"
        ],
                           device=device)
        losses = model.fit(loader,
                           val_dataset,
                           epochs=self.no_of_epochs,
                           learning_rate=self.learning_rate,
                           verbose=True)
        plt.plot(losses)
        plt.show()
        return model
Beispiel #6
0
 def train(self):
     dataset = core.Dataset('images/')
     model = core.Model(['fit_avocado', 'unfit_avocado'])
     model.fit(dataset)
     self.model = model
Beispiel #7
0
from detecto import core, utils, visualize

dataset = core.Dataset('images/')
model = core.Model(['fit_avocado', 'unfit_avocado'])
model.fit(dataset)

image = utils.read_image('images/10.jpg')
predictions = model.predict(image)

# predictions format: (labels, boxes, scores)
labels, boxes, scores = predictions
model.save('avocado_weights.pth')
Beispiel #8
0
from detecto import core, utils, visualize

dataset = core.Dataset('jpegs/')
model = core.Model('Drone')

model.fit(dataset)
Beispiel #9
0
import cv2
from detecto import core, utils, visualize
import numpy as np

dataset = core.Dataset(
    'C:/Users/ABC/Desktop/New folder/TDAI/Data/sample')  #load data
print(len(dataset))
model = core.Model(['top_left', 'top_right', 'bottom_left',
                    'bottom_right'])  #download model

losses = model.fit(dataset, epochs=30, verbose=True,
                   learning_rate=0.001)  #set parameter formodel and fit

model.save('id_card_4_conner.pth')

frame = ''
image = utils.read_image(frame)
labels, boxes, score = model.predict(image)
print(labels)
print(boxes)

##draw boxes
for i, box in enumerate(boxes):
    box = list(map(int, box))
    x_min, y_min, x_max, y_max = box
    cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
    cv2.putText(image, labels[i], (x_min, y_min), cv2.FONT_HERSHEY_COMPLEX,
                0.5, (0, 255, 0))


#nonmax suppression
Beispiel #10
0
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')

<<<<<<< HEAD
from detecto import core, utils, visualize
from torchvision import transforms

augmentations = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomHorizontalFlip(0.5),
    transforms.ColorJitter(saturation=0.5),
    transforms.ToTensor(),
    utils.normalize_transform(),
])


dataset = core.Dataset('labels/',transform=augmentations)
model = core.Model(['baseball',
					  'beer',
					  'can_coke',
					  'can_pepsi',
					  'can_fanta',
					  'can_sprite',
					  'chips_can',
					  'coffee_box',
					  'cracker',
					  'cup',
					  'donut',
					  'fork',
					  'gelatin',
					  'meat',
					  'mustard',
Beispiel #11
0
# Testing out the pre-trained model to see if it detects anything in the screenshot
image = utils.read_image(path + images[0])
model = core.Model()

labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)

# The pre-trained model failed as expected. Though it did detect one of the tiles as a 'book'.

# << Label screenshots with labelImg >>

# In[10]:

# Train model with custom labels
dataset = core.Dataset(path)
model = core.Model(['tile'])
model.fit(dataset)

# In[5]:

# Getting all the test images
path = 'images/test/'
images = []
for file in os.listdir(path):
    if file.endswith(".png"):
        images.append(file)
images

# In[6]:
Beispiel #12
0
            else:
                boxes2 = torch.cat([boxes2,boxes[i]],-1)
            scores2.append(scores[i])
    boxes2 = boxes2.view(-1, 4)

    return labels2, boxes2, scores2
    
    
    
    from detecto import core, utils, visualize

image = utils.read_image('../Label Fotos/COMBINED_DATA/IMG_012674.jpg')
model = core.Model()

labels, boxes, scores = model.predict_top(image)

show_labeled_image(image, boxes, labels)


from detecto import core, utils, visualize

dataset = core.Dataset('/content/drive/My Drive/AEC Hackathon - LifeSavers/Label Fotos/COMBINED_DATA')
print(len(dataset))
#model = core.Model(['person','helmet','nohelmet','nowest'])
model = core.Model(['person'])
model.fit(dataset)
print("Training done")

#model = core.Model.load('safetyheroes_model_v3.pth', ['person'])
#print("loaded")
from detecto import core, utils, visualize
import matplotlib.pyplot as plt

dataset = core.Dataset('frames/')
model = core.Model(['enemy'])

model.fit(dataset, verbose=True)
model.save('model.pth')