Example #1
0
def evalnet(net, signals, labels, sequences, epoch, plot_result={}):
    # net.eval()
    confusion_mat = np.zeros((opt.label, opt.label), dtype=int)
    for i in range(int(len(sequences) / opt.batchsize)):
        signal, label = transformer.batch_generator(
            signals, labels,
            sequences[i * opt.batchsize:(i + 1) * opt.batchsize])
        signal = transformer.ToInputShape(signal, opt, test_flag=True)
        signal, label = transformer.ToTensor(signal,
                                             label,
                                             no_cuda=opt.no_cuda)
        with torch.no_grad():
            out = net(signal)
        pred = torch.max(out, 1)[1]

        pred = pred.data.cpu().numpy()
        label = label.data.cpu().numpy()
        for x in range(len(pred)):
            confusion_mat[label[x]][pred[x]] += 1

    recall, acc, sp, err, k = statistics.report(confusion_mat)
    plot_result['test'].append(err)
    heatmap.draw(confusion_mat, opt, name='current_test')
    print(
        'epoch:' + str(epoch), ' macro-prec,reca,F1,err,kappa: ' +
        str(statistics.report(confusion_mat)))
    return plot_result, confusion_mat
Example #2
0
def evalnet(net, signals, stages, epoch, plot_result={}):
    # net.eval()
    confusion_mat = np.zeros((opt.label, opt.label), dtype=int)
    for i, (signal, stage) in enumerate(zip(signals, stages), 1):

        signal = transformer.ToInputShape(signal,
                                          opt.model_name,
                                          test_flag=True)
        signal, stage = transformer.ToTensor(signal,
                                             stage,
                                             no_cuda=opt.no_cuda)
        with torch.no_grad():
            out = net(signal)
        pred = torch.max(out, 1)[1]

        pred = pred.data.cpu().numpy()
        stage = stage.data.cpu().numpy()
        for x in range(len(pred)):
            confusion_mat[stage[x]][pred[x]] += 1

    recall, acc, sp, err, k = statistics.result(confusion_mat)
    plot_result['test'].append(err)
    heatmap.draw(confusion_mat, opt.label_name, opt.label_name, name='test')
    print('recall,acc,sp,err,k: ' + str(statistics.result(confusion_mat)))
    return plot_result, confusion_mat
Example #3
0
def statistics(mat, opt, logname, heatmapname):
    util.writelog(
        '------------------------------ ' + logname +
        ' result ------------------------------', opt, True)
    util.writelog(
        logname + ' -> macro-prec,reca,F1,err,kappa: ' + str(report(mat)), opt,
        True)
    util.writelog('confusion_mat:\n' + str(mat) + '\n', opt, True)
    heatmap.draw(mat, opt, name=heatmapname)
Example #4
0
def c(data):
	global directionz, currentTarget, snelheid
	if img is None:
		return
	
	print currentTarget.__name__
	targets = [x for x in data.blobs if currentTarget(getat(img, x))]
	heatmap.cooldown(target_heatmap)
	heatmap.draw(target_heatmap, targets)
	hm2 = 1 * target_heatmap
	hm2[hm2 < 80] = 0
	
	f = cv.fromarray(hm2)
	hm_pub.publish(bridge.cv_to_imgmsg(f))
	hm_enc_pub.publish("16SC1")
	twist = Twist()
	# When a dot in the activation matrix is high enough we start to fly towards this place
	if hm2.max() > 16:
		snelheid = 0
		weights = np.apply_along_axis(np.sum, 0, hm2)
		if np.sum(hm2) > 30000:
			# found!
			target_heatmap[:] = 0
			if currentTarget in nextTarget:
				currentTarget = nextTarget[currentTarget]
			else:
				# Start landing
				twist.linear.x = -.1
				action.publish(twist)
				l = rospy.Publisher("/ardrone/land", Empty)
				l.publish(Empty())
				print "found"
				currentTarget = finished
		loc = np.average(indexes, weights=weights)/len(indexes) # When the target if farther to the left or right, we turn faster
		# Print what direction we are going to fly towards
		if loc < .5:
			print "left (new)"
		else:
			print "right (new)"
		twist.linear.x = .05
		twist.angular.z = .5 - loc
	else:
		snelheid = max(snelheid + .001, .05)
		twist.angular.z = snelheid
	action.publish(twist)
Example #5
0
def evalnet(net,
            signals,
            stages,
            sequences,
            epoch,
            plot_result={},
            mode='part'):
    # net.eval()
    if mode == 'part':
        transformer.shuffledata(signals, stages)
        signals = signals[0:int(len(stages) / 2)]
        stages = stages[0:int(len(stages) / 2)]

    confusion_mat = np.zeros((5, 5), dtype=int)
    for i, sequence in enumerate(sequences, 1):

        signal = transformer.ToInputShape(signals[sequence],
                                          opt.model_name,
                                          test_flag=True)
        signal, stage = transformer.ToTensor(signal,
                                             stages[sequence],
                                             no_cuda=opt.no_cuda)
        with torch.no_grad():
            out = net(signal)
        pred = torch.max(out, 1)[1]

        pred = pred.data.cpu().numpy()
        stage = stage.data.cpu().numpy()
        for x in range(len(pred)):
            confusion_mat[stage[x]][pred[x]] += 1
    if mode == 'part':
        plot_result['test'].append(statistics.result(confusion_mat)[0])
    else:
        recall, acc, error = statistics.result(confusion_mat)
        plot_result['test'].append(recall)
        heatmap.draw(confusion_mat, name='test')
        print('test avg_recall:', '%.4f' % recall, 'avg_acc:', '%.4f' % acc,
              'error:', '%.4f' % error)
        #util.writelog('epoch:'+str(epoch)+'  test avg_recall:'+str(round(recall,4))+'  avg_acc:'+str(round(acc,4))+'  error:'+str(round(error,4)))
    return plot_result, confusion_mat
Example #6
0
def c(data):
	global directionz, currentTarget, snelheid
	if img is None:
		return
	lights = [x for x in data.blobs if getat(img, x)[0] > 100]
	if lights:
		naar = min(lights, key=lambda x: x.y)
		twist = Twist()
		twist.linear.x = .1
		if naar.x < 50:
			twist.angular.z = .7
			directionz = 1
			print "hard left"
		elif naar.y > WIDTH - 50:
			twist.angular.z = -.7
			directionz = -1
			print "hard right"
		elif naar.x < MIDPOINTX - 20:
			twist.angular.z = .3
			directionz = 1
			print "left"
		elif naar.x > MIDPOINTX + 20:
			twist.angular.z = -.3
			directionz = -1
			print "right"
		else:
			#twist.linear.x = .2
			print "straight on"
		action.publish(twist)
	else:
		twist = Twist()
		twist.angular.z = directionz
		#action.publish(twist)
		#print "n/a"
	print currentTarget.__name__
	targets = [x for x in data.blobs if currentTarget(getat(img, x))]
	heatmap.cooldown(target_heatmap)
	heatmap.draw(target_heatmap, targets)
	hm2 = 1 * target_heatmap
	hm2[hm2 < 80] = 0
	
	f = cv.fromarray(hm2)
	#print f, dir(f), f.step, f.channels, f.cols, f.rows, f.width, f.height
	hm_pub.publish(bridge.cv_to_imgmsg(f))
	hm_enc_pub.publish("16SC1")
	if targets:
		target = max(targets, key=lambda x: x.area)
		print target.area
		if target.area > 2500:
			#found!
			target_heatmap[:] = 0

			# If we have a new target after this one, take the next target. Otherwise we start landing
			if currentTarget in nextTarget:
				currentTarget = nextTarget[currentTarget]
			else:
				# Start landing
				twist = Twist()
				twist.linear.x = -.1
				action.publish(twist)
				l = rospy.Publisher("/ardrone/land", Empty)
				l.publish(Empty())
				print "found!"
				currentTarget = finished
	twist = Twist()
	if hm2.max() > 16:
		weights = np.apply_along_axis(np.sum, 0, hm2)
		loc = np.average(indexes, weights=weights)/len(indexes)
		if loc < .5:
			print "left (new)"
		else:
			print "right (new)"
		twist.linear.x = .1
		twist.angular.z = .5 - loc# - .5
	action.publish(twist)
Example #7
0
            out = net(signal)
            loss = criterion(out, stage)
            pred = torch.max(out, 1)[1]
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            pred = pred.data.cpu().numpy()
            stage = stage.data.cpu().numpy()
            for x in range(len(pred)):
                confusion_mat[stage[x]][pred[x]] += 1
            if i % show_freq == 0:
                plot_result['train'].append(
                    statistics.result(confusion_mat)[0])
                heatmap.draw(confusion_mat, name='train')
                # plot_result=evalnet(net,signals_eval,stages_eval,plot_result,show_freq,mode = 'part')
                statistics.show(plot_result, epoch + i / (batch_length * 0.8))
                confusion_mat[:] = 0

        plot_result, confusion_mat = evalnet(net,
                                             signals,
                                             stages,
                                             test_sequences[fold],
                                             epoch + 1,
                                             plot_result,
                                             mode='all')
        confusion_mats.append(confusion_mat)
        # scheduler.step()

        if (epoch + 1) % opt.network_save_freq == 0:
Example #8
0
def c(data):
    global directionz, currentTarget, snelheid
    if img is None:
        return
    lights = [x for x in data.blobs if getat(img, x)[0] > 100]
    if lights:
        naar = min(lights, key=lambda x: x.y)
        twist = Twist()
        twist.linear.x = .1
        if naar.x < 50:
            twist.angular.z = .7
            directionz = 1
            print "hard left"
        elif naar.y > WIDTH - 50:
            twist.angular.z = -.7
            directionz = -1
            print "hard right"
        elif naar.x < MIDPOINTX - 20:
            twist.angular.z = .3
            directionz = 1
            print "left"
        elif naar.x > MIDPOINTX + 20:
            twist.angular.z = -.3
            directionz = -1
            print "right"
        else:
            #twist.linear.x = .2
            print "straight on"
        action.publish(twist)
    else:
        twist = Twist()
        twist.angular.z = directionz
        #action.publish(twist)
        #print "n/a"
    print currentTarget.__name__
    targets = [x for x in data.blobs if currentTarget(getat(img, x))]
    heatmap.cooldown(target_heatmap)
    heatmap.draw(target_heatmap, targets)
    hm2 = 1 * target_heatmap
    hm2[hm2 < 80] = 0

    f = cv.fromarray(hm2)
    #print f, dir(f), f.step, f.channels, f.cols, f.rows, f.width, f.height
    hm_pub.publish(bridge.cv_to_imgmsg(f))
    hm_enc_pub.publish("16SC1")
    if targets:
        target = max(targets, key=lambda x: x.area)
        print target.area
        if target.area > 2500:
            #found!
            target_heatmap[:] = 0

            # If we have a new target after this one, take the next target. Otherwise we start landing
            if currentTarget in nextTarget:
                currentTarget = nextTarget[currentTarget]
            else:
                # Start landing
                twist = Twist()
                twist.linear.x = -.1
                action.publish(twist)
                l = rospy.Publisher("/ardrone/land", Empty)
                l.publish(Empty())
                print "found!"
                currentTarget = finished
    twist = Twist()
    if hm2.max() > 16:
        weights = np.apply_along_axis(np.sum, 0, hm2)
        loc = np.average(indexes, weights=weights) / len(indexes)
        if loc < .5:
            print "left (new)"
        else:
            print "right (new)"
        twist.linear.x = .1
        twist.angular.z = .5 - loc  # - .5
    action.publish(twist)
Example #9
0
            out = net(signal)
            loss = criterion(out, label)
            pred = torch.max(out, 1)[1]
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            pred = pred.data.cpu().numpy()
            label = label.data.cpu().numpy()
            for x in range(len(pred)):
                confusion_mat[label[x]][pred[x]] += 1
            iter_cnt += 1
            if iter_cnt % opt.plotfreq == 0:
                plot_result['train'].append(
                    statistics.report(confusion_mat)[3])
                heatmap.draw(confusion_mat, opt, name='current_train')
                statistics.plotloss(
                    plot_result,
                    epoch + i / (train_sequences.shape[1] / opt.batchsize),
                    opt)
                confusion_mat[:] = 0

        plot_result, confusion_mat_eval = evalnet(net, signals, labels,
                                                  test_sequences[fold],
                                                  epoch + 1, plot_result)
        confusion_mats.append(confusion_mat_eval)

        torch.save(net.cpu().state_dict(),
                   os.path.join(opt.save_dir, 'last.pth'))
        if (epoch + 1) % opt.network_save_freq == 0:
            torch.save(
Example #10
0
        out = net(signal)
        loss = criterion(out, stage)
        pred = torch.max(out, 1)[1]
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        pred = pred.data.cpu().numpy()
        stage = stage.data.cpu().numpy()
        for x in range(len(pred)):
            confusion_mat[stage[x]][pred[x]] += 1
        if i % show_freq == 0:
            plot_result['train'].append(statistics.result(confusion_mat)[3])
            heatmap.draw(confusion_mat,
                         opt.label_name,
                         opt.label_name,
                         name='train')
            statistics.show(plot_result, epoch + i / (batch_length * 0.8))
            confusion_mat[:] = 0

    plot_result, confusion_mat = evalnet(net, signals_test, labels_test,
                                         epoch + 1, plot_result)
    confusion_mats.append(confusion_mat)
    # scheduler.step()

    torch.save(net.cpu().state_dict(), './checkpoints/last.pth')
    if (epoch + 1) % opt.network_save_freq == 0:
        torch.save(
            net.cpu().state_dict(), './checkpoints/' + opt.model_name +
            '_epoch' + str(epoch + 1) + '.pth')
        print('network saved.')