コード例 #1
0
    def beginAccTest(self):
        data = pd.read_csv(self.TEST_LIST)
        parsedata = ParseData()
        with open(self.SETTINGS) as fp:
            content = json.load(fp)

            shape = content['shape']
            servo_pred = NNTools(content["servo_setting"])
            servo_pred.load_model(content['servo_model'])

        servo_count = 0

        for index in range(len(data)):
            _, servo, motor = parsedata.parse_data(data["image"][index])

            pred_servo = servo_pred.predict(data["image"][index])

            if abs(servo - pred_servo) <= content['error_margin']:
                # print(servo)
                servo_count += 1
        totalAcc = (100 * servo_count / (index + 1))
        return totalAcc
コード例 #2
0
# read the csv and parse it for prediction
data = pd.read_csv(TEST_LIST)
parsedata = ParseData()
# load settings from json file
with open(SETTINGS) as fp:
    content = json.load(fp)

    shape = content['shape']
    servo_pred = NNTools(content["servo_setting"])
    servo_pred.load_model(content['servo_model'])

servo_count = 0
accum_time = 0
# make predictions from csv and print value
for index in range(len(data)):
    _, servo, motor = parsedata.parse_data(data["image"][index])
    start_time = time.time()
    pred_servo = servo_pred.predict(data["image"][index])
    end_time = time.time()
    accum_time += (end_time - start_time)
    if abs(servo - pred_servo) <= content['error_margin']:
        # print(servo)
        servo_count += 1
    # if (servo-15)*(pred_servo-15) >= 0:
    #     # print(servo)
    #     servo_count += 1

    if (index + 1) % 100 == 0:
        print("[%5d] servo: %2.2f " % \
              (index + 1, 100 * servo_count / (index + 1), ))
コード例 #3
0
class Datagen(Dataset):

    #--------------------------------------------------------------------------
    # method: constructor
    #
    # arguments:
    #  dfile:
    #
    # return: none
    #
    def __init__(self, ilist=None, shape=SHAPE):

        self.transform = transforms.Compose([transforms.ToTensor()])

        self.ilist = ilist
        self.shape = shape

        self.parsedata = ParseData()

        return None

    #
    # end of method

    #--------------------------------------------------------------------------
    # method: get_image
    #
    # arguments:
    #  ifile: input image file
    #
    # return:
    #  image: image tensor
    #
    # This method returns image tensor
    #
    def get_image(self, ifile):

        image = self.parsedata.parse_image(ifile)
        image = cv2.resize(image, (self.shape[0], self.shape[1]), \
                           interpolation=cv2.INTER_CUBIC)
        image = self.transform(image)
        return image.unsqueeze(0)

    #
    # end of method

    #--------------------------------------------------------------------------
    # method: getitem
    #
    # arguments:
    #  index: index for list of images
    #
    # return: image, servo, motor
    #
    # This method returns data
    #
    def __getitem__(self, index):

        # parse image, servo and motor data
        image, servo, motor = self.parsedata.parse_data(self.ilist[index])

        # convert image, servo and motor data to tensor
        image = cv2.resize(image, (self.shape[0], self.shape[1]), \
                           interpolation=cv2.INTER_CUBIC)
        image = self.transform(image)
        servo = torch.tensor(float(servo))
        motor = torch.tensor(float(motor))

        return image, servo, motor

    #
    # end of method

    #--------------------------------------------------------------------------
    # method: length
    #
    # arguments: none
    #
    # return: none
    #
    # This method returns length of data
    #
    def __len__(self):

        return len(self.ilist)

    #
    # end of method


#------------------------------------------------------------------------------
# Debugging Block ANI717
#------------------------------------------------------------------------------
#from torch.utils.data import DataLoader
#
#file = 'data/list/list_1.csv'
#
#dataloader = DataLoader(dataset=Datagen(file,shape=[10,8]), batch_size=1000, \
#                        shuffle=True)
#for image, servo, motor in dataloader:
#    print(image.shape)
#    print(servo, motor)
コード例 #4
0
class DataTest:

    #--------------------------------------------------------------------------
    # method: constructor
    #
    # arguments:
    #  dfile: image data holder file
    #  dwidth: width of the images
    #  dheight: height of the images
    #
    # return: none
    #
    def __init__(self, ifile=IMAGE_LIST, sfile=SETTINGS):

        self.data = pd.read_csv(ifile)
        self.len = len(self.data)
        self.parsedata = ParseData()
        self.badindex = []

        with open(sfile) as fp:
            content = json.load(fp)

            self.shape = content['shape']
            self.servo_pred = NNTools(content["servo_setting"])
            self.motor_pred = NNTools(content["motor_setting"])
            self.servo_pred.load_model(content['servo_model'])
            self.motor_pred.load_model(content['motor_model'])

        return None

    #
    # end of method

    #--------------------------------------------------------------------------
    # method: display_pred
    #
    # arguments:
    #  index: index value
    #
    # return: none
    #
    # This method compares prediction with given data
    #
    def display_pred(self, index=0):

        # parse image, servo and motor data
        image, servo, motor = self.parsedata.parse_data(
            self.data["image"][index])

        # make prediction
        pred_servo = self.servo_pred.predict(self.data["image"][index])
        pred_motor = self.motor_pred.predict(self.data["image"][index])

        # process image with servo and motor values for displey
        image = cv2.resize(image, (self.shape[0], self.shape[1]), \
                           interpolation=cv2.INTER_CUBIC)
        cv2.line(image, (240, 280), (240 - 20*(15 - servo), 100), \
                                     (255, 0, 0), 3)
        cv2.line(image, (220, 280), (220 - 20*(15 - pred_servo), 100), \
                                     (255, 255, 0), 3)
        image = cv2.putText(image, str(pred_motor) + ":" + str(motor), \
                                (180, 310), cv2.FONT_HERSHEY_SIMPLEX, 1, \
                                    (255,255,255), 2, cv2.LINE_AA)

        # show image
        cv2.imshow('picture {}'.format(index), cv2.cvtColor(image, \
                                   cv2.COLOR_RGB2BGR))
        cv2.moveWindow('picture {}'.format(index), 500, 500)

        return None

    #
    # end of method

    #--------------------------------------------------------------------------
    # method: display
    #
    # arguments:
    #  index: index value
    #  key: provided key press
    #  nfile: new csv file to store refined images
    #
    # return: none
    #
    # This method displays images, also lets user to refine image set by
    # deleting unwanted images
    #
    def display(self, nfile=NEW_LIST, index=0, key=SPACE_KEY):

        # when 'd' is pressed, (index-1) value is appended in badindex array
        # when 's' is pressed, all image data except deleted indices is saved
        # to new CSV image data file
        if (key == DEL_KEY):
            self.badindex.append(index - 1)
            cv2.destroyWindow('picture {}'.format(index - 1))
        elif (key == SAVE_KEY):
            ref_data = self.data[0:index].drop(self.badindex)
            ref_data.to_csv(nfile, index=False)
        else:
            cv2.destroyWindow('picture {}'.format(index - 1))

        # parse image, servo and motor data
        image, servo, motor = self.parsedata.parse_data(
            self.data["image"][index])

        # process image with servo and motor values for displey
        image = cv2.resize(image, (self.shape[0], self.shape[1]), \
                           interpolation=cv2.INTER_CUBIC)
        cv2.line(image, (240, 300), (240 - 20*(15 - servo), 200), \
                             (255, 0, 0), 3)
        image = cv2.putText(image, str(motor), (180, 310),  \
                                  cv2.FONT_HERSHEY_SIMPLEX, \
                                     1, (255,255,255), 2, cv2.LINE_AA)

        # show image
        cv2.imshow('picture {}'.format(index), cv2.cvtColor(image, \
                               cv2.COLOR_RGB2BGR))
        cv2.moveWindow('picture {}'.format(index), 500, 500)

        return None

    #
    # end of method


#------------------------------------------------------------------------------
# Debugging Block ANI717
#------------------------------------------------------------------------------
#import timeit
#QUIT_KEY = ord('q')
#total_start = timeit.default_timer()
#
#cardata = CarDataFile()
#for i in range(100):
#    cardata.display_pred(index=i)
#
#total_stop = timeit.default_timer()
#print('Total time required: %f' % (total_stop - total_start))