Esempio n. 1
0
 def create_data(self, inputs, targets):
     data = SequentialDataSet(inputs, targets)
     for i in xrange(0, len(self.dataframe) - 1):
         data.newSequence()
         ins = self.dataframe.ix[i].values
         target = self.dataframe.ix[i + 1].values[0]
         data.appendLinked(ins, target)
     self.data = data
Esempio n. 2
0
 def create_data(self, inputs, targets):
     data = SequentialDataSet(inputs, targets)
     for i in xrange(0, len(self.dataframe) - 1):
         data.newSequence()
         ins = self.dataframe.ix[i].values
         target = self.dataframe.ix[i + 1].values[0]
         data.appendLinked(ins, target)
     self.data = data
Esempio n. 3
0
def create_data(dataframe, inputs, targets):
    data = SequentialDataSet(inputs, targets)
    for i in range(0, dataframe.shape[0] - 1):
        row = dataframe.iloc[i]
        data.newSequence()
        ins = row.values
        target = dataframe.iloc[i + 1].values[0]
        data.appendLinked(ins, target)
    return data
def rnnTrain(data):

    ds = SequentialDataSet(3,3)
    s = np.size(input) / 9

    ds.newSequence()
    for idx1 in range(s):
        ds.appendLinked(data[idx1],(1,0,0))

    ds.newSequence()
    for idx2 in range(s):
        ds.appendLinked(data[idx2+s],(0,1,0))

    ds.newSequence()
    for idx3 in range(s):
        ds.appendLinked(data[idx3+s+s],(0,0,1))

    net = buildNetwork(3, 8, 3, bias=True, recurrent=True, hiddenclass=LSTMLayer)
    trainer = BackpropTrainer(net,ds)
    trainer.trainEpochs(1000)

    return net
Esempio n. 5
0
consecutive_days_pressure_dropping_by = 0
ds = SequentialDataSet(5, 1)
for sample, next_sample in zip(training_features, cycle(training_features[1:])):
    ds.newSequence()
    yesterdays_sample = sample
    yesterdays_pressure = yesterdays_sample[3]
    todays_pressure = next_sample[3]
    raining = 0.0
    if (todays_pressure > yesterdays_pressure):
        consecutive_days_pressure_dropping_by += (todays_pressure - yesterdays_pressure)
    else:
        raining = 1.0
        consecutive_days_pressure_dropping_by = 0
    yesterdays_sample.append(float(consecutive_days_pressure_dropping_by))
    ds.appendLinked(yesterdays_sample, raining)

net = buildNetwork(5, 10, 1,
                   hiddenclass=LSTMLayer, outputbias=False, recurrent=True)

#inLayer = LinearLayer(2)
#hiddenLayer = LSTMLayer(5)
#outLayer = LinearLayer(1)
#from pybrain.structure import FullConnection
#in_to_hidden = FullConnection(inLayer, hiddenLayer)
#hidden_to_out = FullConnection(hiddenLayer, outLayer)

trainer = RPropMinusTrainer(net, dataset=ds)
train_errors = [] # save errors for plotting later
EPOCHS_PER_CYCLE = 5
CYCLES = 50
Esempio n. 6
0
class LanguageLearner:

	__OUTPUT = "Sample at {0} epochs (prompt=\"{1}\", length={2}): {3}"

	def __init__(self, trainingText, hiddenLayers, hiddenNodes):
		self.__initialized = False
		with open(trainingText) as f:
			self.raw = f.read()
		self.characters = list(self.raw)
		self.rawData = list(map(ord, self.characters))
		print("Creating alphabet mapping...")
		self.mapping = []
		for charCode in self.rawData:
			if charCode not in self.mapping:
				self.mapping.append(charCode)
		print("Mapping of " + str(len(self.mapping)) + " created.")
		print(str(self.mapping))
		print("Converting data to mapping...")
		self.data = []
		for charCode in self.rawData:
			self.data.append(self.mapping.index(charCode))
		print("Done.")
		self.dataIn = self.data[:-1:]
		self.dataOut = self.data[1::]
		self.inputs = 1
		self.hiddenLayers = hiddenLayers
		self.hiddenNodes = hiddenNodes
		self.outputs = 1

	def initialize(self, verbose):
		print("Initializing language learner...")
		self.verbose = verbose

		# Create network and modules
		self.net = RecurrentNetwork()
		inp = LinearLayer(self.inputs, name="in")
		hiddenModules = []
		for i in range(0, self.hiddenLayers):
			hiddenModules.append(LSTMLayer(self.hiddenNodes, name=("hidden-" + str(i + 1))))
		outp = LinearLayer(self.outputs, name="out")

		# Add modules to the network with recurrence
		self.net.addOutputModule(outp)
		self.net.addInputModule(inp)
		
		for module in hiddenModules:
			self.net.addModule(module)

		# Create connections

		self.net.addConnection(FullConnection(self.net["in"], self.net["hidden-1"]))
		for i in range(0, len(hiddenModules) - 1):
			self.net.addConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 2)]))
			self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 1)]))
		self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))],
			self.net["hidden-" + str(len(hiddenModules))]))
		self.net.addConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))], self.net["out"]))
		self.net.sortModules()

		self.trainingSet = SequentialDataSet(self.inputs, self.outputs)
		for x, y in zip(self.dataIn, self.dataOut):
			self.trainingSet.newSequence()
			self.trainingSet.appendLinked([x], [y])

		self.net.randomize()

		print("Neural network initialzed with structure:")
		print(self.net)

		self.trainer = BackpropTrainer(self.net, self.trainingSet, verbose=verbose)
		self.__initialized = True
		print("Successfully initialized network.")

	def train(self, epochs, frequency, prompt, length):
		if not self.__initialized:
			raise Exception("Attempted to train uninitialized LanguageLearner")
		print ("Beginning training for " + str(epochs) + " epochs...")
		if frequency >= 0:
			print(LanguageLearner.__OUTPUT.format(0, prompt, length, self.sample(prompt, length)))
		for i in range(1, epochs):
			print("Error at " + str(i) + " epochs: " + str(self.trainer.train()))
			if i % frequency == 0:
				print(LanguageLearner.__OUTPUT.format(i, prompt, length, self.sample(prompt, length)))
		print("Completed training.")

	def sample(self, prompt, length):
		self.net.reset()
		if prompt == None:
			prompt = chr(random.choice(self.mapping))
		output = prompt
		charCode = ord(prompt)
		for i in range(0, length):
			sampledResult = self.net.activate([charCode])
			charCode = int(round(sampledResult[0]))
			if charCode < 0 or charCode >= len(self.mapping):
				return output + "#TERMINATED_SAMPLE(reason: learner guessed invalid character)"
			output += chr(self.mapping[charCode])
		return output
Esempio n. 7
0
files = glob.glob('reel_nopickup/*.csv') #16th notes
tunes = []
for filename in files: #tune
	with open(filename) as f:
		notes = map(int, map(str.strip, f.readlines()))[::2] #8th ntoes
		bitmasks = map(to_bitmask, notes)
		tunes.append(bitmasks)

nested_tunes = map(lambda tune: [tune[x:min(len(tune) + 1, x+4)] for x in range(0, len(tune), 4)], tunes)


for tune in nested_tunes:
	for (inp, target) in zip(tune, tune[1:]):
		chord_ds.newSequence()
		chord_ds.appendLinked(inp[0], target[0])

for tune in tunes:
	for beat in tunes:
		for (inp, target) in zip(beat, beat[1:]):
			melody_ds.newSequence()
			melody_ds.appendLinked(inp, target)

chord_network.randomize()
melody_network.randomize()

chord_trainer = BackpropTrainer(chord_network, chord_ds, learningrate=.00001, momentum=.9)
melody_trainer = BackpropTrainer(melody_network, melody_ds, learningrate=.00001, momentum=.9)
import time
print "training chords..."
print "\n".join(map(str, chord_trainer.trainUntilConvergence()))
Esempio n. 8
0
from pybrain.datasets import SequentialDataSet
from pybrain.structure import SigmoidLayer
from pybrain.structure import LSTMLayer

import itertools
import numpy as np

data = np.loadtxt("datain.txt").T
print data

datain = data[:-1,:] 
dataout = data[1:,:] 

INPUTS = 5
OUTPUTS = 5
HIDDEN = 40

net = buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True) 

ds = SequentialDataSet(INPUTS, OUTPUTS)

for x,y in itertools.izip(datain,dataout):
    ds.newSequence()
    ds.appendLinked(tuple(x), tuple(y))

net.randomize()

trainer = BackpropTrainer(net, ds)

for _ in range(1000):
    print trainer.train()
class smokeDetect(AbstractVisionModule):
    """example vision class. Detects faces using the openCV face detector"""
    def __init__(self,
                 host,
                 port,
                 update_frequency,
                 source="webcam",
                 verbose=False):
        self.logger = logging.getLogger("Borg.Brain.Vision.smokeDetect")
        super(smokeDetect, self).__init__(host, port)
        self.update_frequency = update_frequency
        self.__ticker = Ticker(frequency=update_frequency)

        self.verbose = verbose

        self.source = [source]
        self.vid_mem_reader = util.vidmemreader.VidMemReader(self.source)
        # set the type of objects that someone can detect
        self.set_known_objects(['smoke'])

        self.windowLocations = []

        self.labels = []
        self.data = []

        self.frameAverage = 5
        self.svm = None
        self.load_svm()

    def __del__(self):
        cv2.destroyAllWindows()

    def init_nn(self, datain, dataout):
        INPUTS = 5
        OUTPUTS = 1
        HIDDEN = 20
        self.net = buildNetwork(INPUTS,
                                HIDDEN,
                                OUTPUTS,
                                hiddenclass=LSTMLayer,
                                outclass=SigmoidLayer,
                                recurrent=True,
                                bias=True)
        self.ds = SequentialDataSet(INPUTS, OUTPUTS)

        for x, y in itertools.izip(datain, dataout):
            self.ds.newSequence()
            self.ds.appendLinked(tuple(x), tuple(y))

        self.net.randomize()

        trainer = BackpropTrainer(self.net, self.ds)

        for _ in range(1000):
            print trainer.train()

    def train(self):
        pass

    def stop(self):
        cv2.destroyAllWindows()

    def get_vid(self):
        if not hasattr(self, 'vid_idx'):
            self.vid_idx = 0
        else:
            self.vid_idx += 1
            if self.vid_idx >= len(vid_files):
                self.vid_idx = 0

        print "NOW LOADING ", vid_files[self.vid_idx]
        return cv2.VideoCapture(vid_files[self.vid_idx])

    def get_image_from_video(self, vid, size=(320, 240)):
        (result, image) = vid.read()
        if not result:
            #vid.release()
            vid = self.get_vid()
            (_, image) = vid.read()
        image = cv2.resize(image, size)
        return image

    def run(self):
        """start loop which gets a new image, then processes it"""
        #cv2.namedWindow("color_hist", cv2.cv.CV_WINDOW_NORMAL)
        im = None
        while im == None:
            im = self.vid_mem_reader.get_latest_image()
            if im == None:
                print "not receiving images yet..."
                time.sleep(0.2)

        vid = self.get_vid()
        idx = -1
        while True:
            histogramList = []
            imageList = []
            colorList = []
            imageOrigList = []
            croppedList = []
            timeBlock = time.time()
            heartBeat = time.time()
            idx = -1
            while time.time() - timeBlock < 3:
                idx += 1
                if idx > 300:
                    break
                #Getting pictures for 5 seconds
                if time.time() - heartBeat > 2:
                    self._AbstractVisionModule__send_heartbeat()
                    heartBeat = time.time()

                #temp = self.vid_mem_reader.get_latest_image()[0]
                #image = np.asarray(temp[:,:])

                #image = self.get_image_from_video(vid)
                (result, image) = vid.read()
                if not result:
                    #vid.release()
                    vid = self.get_vid()
                    (_, image) = vid.read()
                image = cv2.resize(image, (320, 240))
                if self.vid_idx == 0:
                    w, h, _ = image.shape
                    image = image[100:240, :, :]
                image = cv2.resize(image, (320, 240))

                image_gs = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                colorList.append(image)
                imageOrigList.append(image_gs)
                image_gs = cv2.blur(image_gs, (15, 15))
                imageList.append(image_gs)

                #Calculating Histogram of Gray scale Image and Normalizing it
                hist = cv2.calcHist(images=[image_gs],
                                    channels=[0],
                                    mask=None,
                                    histSize=[32],
                                    ranges=[0, 256])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                histogramList.append(hist)

                #Calculating Histogram of the Color Image
                curve = hist_curve(image, 128)

                if self.verbose:
                    image_gs_rs = cv2.resize(image_gs, (613, 408))
                    cv2.imshow("Smoothed_Gray_Scale", image_gs_rs)
                    cv2.moveWindow("Smoothed_Gray_Scale", 55, 0)
                    cv2.waitKey(1)

            self._AbstractVisionModule__send_heartbeat()

            contourList = []
            for i in range(len(imageList) - 1):

                if time.time() - heartBeat > 4:
                    self._AbstractVisionModule__send_heartbeat()
                    heartBeat = time.time()

                motion, equalized_motion, denoised_motion, denoised_mask = self.extractMotion(
                    imageList[i], imageList[i + 1])

                #Calculating Normalized Histogram of motion image
                motion_hist = hist_curve(equalized_motion,
                                         bin=64,
                                         mask=denoised_motion)
                y = cv2.resize(motion_hist, (320, 240))

                #Cropped Grayscale image based on motion
                final = np.zeros(image_gs.shape, dtype=np.int8)
                final = cv2.add(final,
                                imageOrigList[i],
                                mask=denoised_mask,
                                dtype=cv2.CV_8UC1)

                #Getting cropped color image
                finalColor = self.cropColor(colorList[i], denoised_mask)
                croppedList.append(finalColor)
                finalOrigColor = colorList[i]

                #Calculating Contours
                contours, hierarchy = cv2.findContours(denoised_mask,
                                                       cv2.RETR_EXTERNAL,
                                                       cv2.CHAIN_APPROX_SIMPLE)

                sanityList = []
                #Accept frames having more than one contour
                if len(contours) > 1:
                    for array in contours:
                        temp_data = []
                        #Calculate contour structures
                        area = cv2.contourArea(array)
                        if area < 100:
                            continue
                        center, radius = cv2.minEnclosingCircle(array)
                        circleArea = math.pi * math.pow(radius, 2)
                        isConvex = cv2.isContourConvex(array)
                        boundingRect = cv2.boundingRect(array)
                        rectArea = boundingRect[2] * boundingRect[2]
                        ellipseRect = cv2.fitEllipse(array)
                        rectArea = ellipseRect[2] * ellipseRect[2]
                        temp_data.extend([area, circleArea, rectArea])
                        temp_data.extend([radius, float(isConvex)])

                        xpos, ypos, w, h = boundingRect
                        contour_hist = cv2.calcHist(
                            images=[
                                equalized_motion[xpos:xpos + w, ypos:ypos + h]
                            ],
                            channels=[0],
                            mask=denoised_motion[xpos:xpos + w, ypos:ypos + h],
                            histSize=[32],
                            ranges=[0, 256])
                        cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)

                        self.logger.debug("contour hist shape : %s",
                                          str(contour_hist.shape))
                        temp_data.extend(
                            np.array(contour_hist).reshape(-1, ).tolist())
                        #Add deficiency information
                        if not isConvex:
                            temp_data.extend(self.convexDeficiency(array))
                        else:
                            #Put zero for all the expected values
                            temp_data.extend([0., 0., 0., 0., 0.])

                        #Ignore big areas and areas which look rectangular
                        if area > 100 and rectArea / area > 2.5:
                            sanityList.append(center)
                            self.data.append(temp_data)
                            #Automatic Labeling based on video
                            if self.vid_idx == 0:
                                self.labels.append(1.0)
                            else:
                                self.labels.append(0.0)

                        test = np.array(temp_data)
                        test = test.astype(np.float32)

                        if self.svm.predict(test) == 1:
                            tempol = np.copy(finalOrigColor)
                            cv2.drawContours(tempol, array, -1, (255, 0, 0))
                            tempol_rs = cv2.resize(tempol, (614, 408))
                            cv2.imshow("Finall", tempol_rs)
                            cv2.moveWindow("Finall", 1800 + 45, 460)
                            cv2.waitKey(1)

                mean = np.mean(np.asarray(sanityList))
                std = np.std(np.asarray(sanityList))
                contourList.append((mean, std, contours))
                if self.verbose:
                    motion_rs = cv2.resize(motion, (613, 408))
                    cv2.normalize(motion_rs, motion_rs, 0, 255,
                                  cv2.NORM_MINMAX)
                    cv2.imshow("motion", motion_rs)

                    cv2.moveWindow("motion", 640 + 45, 0)
                    temp_rs = cv2.resize(denoised_motion, (613, 408))
                    cv2.imshow("equap", temp_rs)
                    cv2.moveWindow("equap", 1280 + 45, 0)
                    finalColor_rs = cv2.resize(finalColor, (613, 408))
                    cv2.imshow("cropped-motion", finalColor_rs)
                    cv2.moveWindow("cropped-motion", 0 + 45, 460)
                    y_rs = cv2.resize(y, (613, 408))
                    cv2.imshow("cropped-motion-hist", y_rs)
                    cv2.moveWindow("cropped-motion-hist", 640 + 45, 460)
                    cv2.waitKey(1)

                    #time.sleep(0.1)
                    '''
                    DISCRETE FOURIER TRANSFORM
                    dft_im = dft(denoised_motion)
                    pyrdown = dft_im
                    for i in range(4):
                        pyrdown = cv2.pyrDown(pyrdown)
                    w,h = dft_im.shape
                    pyrdown = cv2.resize(pyrdown, (h,w))
                    cv2.imshow("pyrdown", pyrdown)
                    '''
                #Scoring detections in frame sequences
                Score = 0
                minStd = 100
                for mean, std, contours in contourList:

                    #print 'Standard deviation of contours', std
                    if std > 0 and std < 50:
                        if std < minStd:
                            minStd = std
                            bestContour = contours
                            minMean = mean
                        Score += 1
                    elif std > 50:
                        Score -= 30
                    elif std > 39:
                        Score -= 1
                    else:
                        #Score -= 1
                        pass
                if Score > 0:
                    self.add_property('name', 'smoke')
                    self.add_property('center', minMean)
                    self.add_property('image_path', '/dev/shm/smoke.png')
                    self.store_observation()

                    tempo = np.copy(finalOrigColor)
                    cv2.drawContours(tempo, contours, -1, (255, 0, 0))
                    cv2.imwrite('/dev/shm/smoke.png', tempo)

                    tempo_rs = cv2.resize(tempo, (614, 408))
                    cv2.imshow("Final", tempo_rs)
                    cv2.moveWindow("Final", 1280 + 45, 460)
                    cv2.waitKey(1)
                    self.update()

            self.save(self.data, self.labels)
            self.update()

    def save(self, data, labels):
        data = np.array(data)
        labels = np.array(labels)

        self.logger.info("Data size is %s", str(data.shape))
        self.logger.info("label size is %s", str(labels.shape))
        np.save("/dev/shm/data", data)
        np.save("/dev/shm/label", labels)

        np.savetxt("/dev/shm/data.txt", data)
        np.savetxt("/dev/shm/label.txt", labels)

    def convexDeficiency(self, contour):
        '''
        Calculates the convex deficiency and returns a list of:
        1- mean and standard deviation of deficiency depth, and length of the starting/ending point of the deficiency
        2- Number of deficiencies
        3- perimiter of each defeciency  
        @contour The contour of the image
        '''
        convex_hull = cv2.convexHull(contour, returnPoints=False)
        convex_defects = cv2.convexityDefects(contour, convex_hull)
        self.logger.debug("convex_defects is: %s", str(convex_defects))

        defect_number = len(convex_defects)
        distance_list = []
        depth_list = []
        premeter_list = []
        if defect_number:

            for defect in convex_defects:
                self.logger.debug("current defect is: %s", str(defect))
                start_idx, end_idx, farthest_pt, pt_depth, = defect[0]
                self.logger.debug("start/end contour points are %s and %s ",
                                  str(contour[start_idx][0]),
                                  str(contour[end_idx][0]))
                distance = math.pow(
                    (contour[start_idx][0][0] - contour[end_idx][0][0]),
                    2) + math.pow(
                        (contour[start_idx][0][1] - contour[end_idx][0][1]), 2)
                distance_list.append(distance)
                depth_list.append(pt_depth)

        mean_distance = np.mean(np.asarray(distance_list))
        std_distance = np.std(np.asarray(distance_list))
        mean_depth = np.mean(np.asarray(depth_list))
        std_depth = np.std(np.asarray(depth_list))

        return [
            mean_distance, std_distance, mean_depth, std_depth, defect_number
        ]

    def cropColor(self, image, mask):
        #Crop a color image based on a mask
        ch1 = np.zeros(mask.shape, dtype=np.int8)
        ch2 = np.zeros(mask.shape, dtype=np.int8)
        ch3 = np.zeros(mask.shape, dtype=np.int8)

        c1 = np.copy(image[:, :, 0])
        c2 = np.copy(image[:, :, 1])
        c3 = np.copy(image[:, :, 2])

        ch1 = cv2.add(ch1, c1, mask=mask, dtype=cv2.CV_8UC1)
        ch2 = cv2.add(ch2, c2, mask=mask, dtype=cv2.CV_8UC1)
        ch3 = cv2.add(ch3, c3, mask=mask, dtype=cv2.CV_8UC1)

        finalColor = cv2.merge((ch1, ch2, ch3))

        return finalColor

    def extractMotion(self, image1, image2):
        #Background Subtraction
        motion = cv2.absdiff(image1, image2)

        #Histogram Equalization of the motion image
        #TODO: Test without equalization and only normalization
        #eqaulized_motion = cv2.equalizeHist(motion)
        #eqaulized_motion = np.zeros(motion.shape)
        eqaulized_motion = cv2.normalize(src=motion,
                                         alpha=0,
                                         beta=255,
                                         norm_type=cv2.NORM_MINMAX)
        #Removing Noise by opening and closing the image
        se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 8))
        temp = cv2.morphologyEx(eqaulized_motion, cv2.MORPH_OPEN, se)

        se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        temp = cv2.morphologyEx(temp, cv2.MORPH_CLOSE, se)

        denoised_motion = temp

        #Making a mask from the closed/opened image
        denoised_mask = np.zeros(image1.shape, dtype=np.uint8)
        denoised_mask[denoised_motion > 70] = 1

        return motion, eqaulized_motion, denoised_motion, denoised_mask

    def deltaHistogram(self, histogramList, image_gs, hist):
        '''
        This function doesn't work, it should calculate delta histograms
        '''
        for i in range(len(histogramList) - 1):
            deltaHist = np.absolute(
                np.subtract(histogramList[i], histogramList[i + 1]))
            deltaHist /= np.sum(deltaHist)

            nans = np.isnan(deltaHist)
            deltaHist[nans] = 0

            deltaImage = np.zeros(image_gs.shape)
            binStep = image_gs.shape[1] / deltaHist.shape[0]

            print deltaHist

            for i in range(len(hist) - 1):
                pt1 = (binStep * i,
                       image_gs.shape[0] - (deltaHist[i] * image_gs.shape[0]))
                pt2 = (binStep * i + 1, image_gs.shape[0] -
                       (deltaHist[i + 1] * image_gs.shape[0]))
                color = 255
                cv2.line(deltaImage, pt1, pt2, color, 2, 8, 0)

            if self.verbose:
                cv2.imshow("delta_hist", deltaImage)
                cv2.moveWindow("delta_hist", 1600, 0)
                cv2.waitKey(10)
            time.sleep(0.1)

    def archive(self, image):
        #Adds all types of image conversion to the specified list
        pass

    def load_svm(self):
        self.svm = cv2.SVM()
        try:
            self.svm.load("/dev/shm/svm.model")
        except:
            self.svm = None
Esempio n. 10
0

#create the training set
#takes word vector -> simple enumerated vector of words from vocab
print("Creating the DataSet")
dataSet = SequentialDataSet(inputLayerSize,outputLayerSize)
dataSet.newSequence()
for sample,next in zip(blob[0:sampleSize],cycle(blob[1:sampleSize+1])):
    try:
        if sample == ' ': dataSet.newSequence()
        #print("creating Sample of:",sample,next)
        actual = [0.0 for x in range(inputLayerSize)]
        actual[wordToNum[sample]] = 1.0
        expected = [0.0 for x in range(inputLayerSize)]
        expected[wordToNum[next]] = 1.0
        dataSet.appendLinked(actual,expected)
    except KeyError as e:
        print("Missing: ",str(e))
        #print("Something went wrong for:",sample,next)

print("Data Set Created: ",dataSet.getNumSequences())

if False: #os.path.exists(networkSaveFile):
    print("Loading Neural Network")
    networkSave = codecs.open(networkSaveFile,'r','utf-8')
    net = pickle.load(networkSave)
    networkSave.close()
else:
    #create the network
    print("Creating Network: ",inputLayerSize,"->",hiddenLayerSize,"->",outputLayerSize)
    net = buildNetwork(inputLayerSize,hiddenLayerSize,outputLayerSize,hiddenclass=LSTMLayer,outputbias=False,recurrent=True)
Esempio n. 11
0
class NET():
    def __init__(self, inputsize, outputsize, hiden=[1]):
        self.inputsize = inputsize
        self.outputsize = outputsize
        self.hiden = hiden
        self.err = 1
        self.old_err = 1
        #print type(self.hiden)
        if type(self.hiden) == str:
            #print "type str"
            self.hiden = self.hiden[1:-1]
            b = self.hiden.split(", ")
            c = []
            for i in b:
                c.append(int(i))
            self.hiden = c[:]
        b = []
        b.append(self.inputsize)
        b += self.hiden
        b.append(self.outputsize)
        #print b#"%s, %s, %s, hiddenclass=TanhLayer"%(self.inputsize, self.hiden, self.outputsize)
        self.net = FeedForwardNetwork()
        self.inputlayer = LinearLayer(self.inputsize, "Input")
        self.net.addInputModule(self.inputlayer)
        self.outputlayer = LinearLayer(self.outputsize, "Output")
        self.net.addOutputModule(self.outputlayer)
        self.hidenlayers = []
        for i in xrange(len(self.hiden)):
            self.hidenlayers.append(SigmoidLayer(self.hiden[i], "hiden%s" % i))
            self.net.addModule(self.hidenlayers[-1])
        self.net.addConnection(
            FullConnection(self.inputlayer, self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            self.net.addConnection(
                FullConnection(self.inputlayer, self.hidenlayers[i]))
            self.net.addConnection(
                FullConnection(self.hidenlayers[i], self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            for j in xrange(i + 1, len(self.hidenlayers)):
                self.net.addConnection(
                    FullConnection(self.hidenlayers[i], self.hidenlayers[j]))
                #self.print_conections(self.net)
        self.net.sortModules()
        self.ds = SupervisedDataSet(self.inputsize, self.outputsize)

    def Update(self, hiden, h):
        self.net = FeedForwardNetwork()
        self.inputlayer = LinearLayer(self.inputsize, "Input")
        self.net.addInputModule(self.inputlayer)
        self.outputlayer = LinearLayer(self.outputsize, "Output")
        self.net.addOutputModule(self.outputlayer)
        self.hidenlayers = []
        for i in xrange(len(hiden)):
            self.hidenlayers.append(SigmoidLayer(hiden[i], "hiden%s" % i))
            self.net.addModule(self.hidenlayers[-1])
        self.net.addConnection(
            FullConnection(self.inputlayer, self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            self.net.addConnection(
                FullConnection(self.inputlayer, self.hidenlayers[i]))
            self.net.addConnection(
                FullConnection(self.hidenlayers[i], self.outputlayer))
        for i in xrange(len(self.hidenlayers)):
            for j in xrange(i + 1, len(self.hidenlayers)):
                if i < h:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j]))
                elif i == h:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j],
                                       inSliceTo=hiden[i] - 1))
                else:
                    self.net.addConnection(
                        FullConnection(self.hidenlayers[i],
                                       self.hidenlayers[j]))
                #self.print_conections(self.net)
        self.net.sortModules()
        self.hiden = hiden

    def print_conections(self, n):
        print("BEGIN")
        for mod in n.modules:
            print(mod)
            for conn in n.connections[mod]:
                print(conn)
                for cc in range(len(conn.params)):
                    print(conn.whichBuffers(cc), conn.params[cc])
        print("END")

    def AddData(self, datainput, dataoutput):
        if len(dataoutput) != len(datainput):
            print("Not equals data", len(dataoutput), len(datainput))
            return 1
        self.ds = SupervisedDataSet(self.inputsize, self.outputsize)
        for i in xrange(len(dataoutput)):
            self.ds.appendLinked(datainput[i], dataoutput[i])
        self.trainer = RPropMinusTrainer(self.net,
                                         dataset=self.ds,
                                         learningrate=0.1)
        return 0

    def AddDataSequential(self, data):
        self.ds = SequentialDataSet(self.inputsize, self.outputsize)
        for i in xrange(len(data) - 1, 0, -1):

            t = data[i]
            k = i - 1
            while k > -1:
                self.ds.appendLinked(data[k], t)
                k -= 1
            self.ds.newSequence()
        """print self.ds.getNumSequences()
		for i in range(self.ds.getNumSequences()):
			for input, target in self.ds.getSequenceIterator(i):
				print i, TransToIntList_45(input), TransToIntList_45(target)"""
        self.trainer = RPropMinusTrainer(self.net,
                                         dataset=self.ds,
                                         learningrate=0.1)
        return 0

    def TrainNet(self, epoch, error):

        if epoch <= 5:
            epoch = 5
        i = 0
        count = 0
        while i < epoch:
            if error == self.err:
                break
            self.err = self.trainer.train()
            if self.err == self.old_err:
                count += 1
            else:
                count = 0
            if count == 3:
                self.err = self.old_err
                return (self.err, 1)
            self.old_err = self.err
            i += 1
        #self.SaveNet('%s  %s_%s_%s.work'%(self.err, self.inputsize, self.hiden, self.outputsize))
        return [self.err, 0]

    def TrainNetOnce(self):

        self.err = self.trainer.train()

        return self.err

    def SaveNet(self, filename=None):
        if filename == None:
            NetworkWriter.writeToFile(
                self.net, '%s  %s_%s_%s.xml' %
                (self.err, self.inputsize, self.hiden, self.outputsize))
        else:
            NetworkWriter.writeToFile(self.net, filename)

    def LoadNet(self, fname):
        self.net = NetworkReader.readFrom(fname)
        tree = ET.parse(fname)
        x = tree.getroot()
        l = []
        for modules in x.findall('Network/Modules/SigmoidLayer/dim'):
            l.append(int(modules.get("val")))
        self.hiden = l[:]
        self.inputsize = self.net.indim
        self.outputsize = self.net.outdim

    def TestNet(self, inp):
        if len(inp) != self.inputsize:
            return 0
        return self.net.activate(inp[:])

    def UpdateWeights(self, f1, f2=None):
        n = NetworkReader.readFrom(f1)
        if f2 != None:
            n2 = NetworkReader.readFrom(f2)

        def DictParams(n):
            l1 = []
            for mod in n.modules:
                l = []
                for conn in n.connections[mod]:

                    if conn.paramdim > 0:

                        l.append([conn.outmod.name, conn.params])
                d = dict(l)
                l1.append([mod.name, d])
            d1 = dict(l1)
            return d1

        d1 = DictParams(n)
        if f2 != None:
            d2 = DictParams(n2)
        d3 = DictParams(self.net)

        params = np.array([])
        if f2 != None:
            for i in d2:
                for j in d2[i]:
                    b = d3[i][j][:]
                    b[:d2[i][j].size] = d2[i][j][:]
                    d3[i].update({j: b})
        for i in d1:
            for j in d1[i]:
                b = d3[i][j][:]
                b[:d1[i][j].size] = d1[i][j][:]
                d3[i].update({j: b})
        for i in d3["Input"]:
            params = np.hstack((params, d3["Input"][i]))
        for i in xrange(len(self.hiden)):
            for j in d3["hiden%s" % i]:
                params = np.hstack((params, d3["hiden%s" % i][j]))
        self.net._setParameters(params)
Esempio n. 12
0
def evalSensor(sensorIndex, featureSpaceIndices):
    #reset data structure
    allByTime = {}
    f = open(fileName, 'r')
    headers = f.readline().split()
    for line in f:
        splited = line.split() 
        timeStamp = int(splited[0])
        allByTime[timeStamp] = {}
    f.close()
    allByJoint = {}
    for inputIndices in featureSpaceIndices:
        allByJoint[inputIndices] = {}
    clfs = {}
    grades = {}
    for inputIndices in featureSpaceIndices:
        allByTime, allByJoint = angleExtraction.prepareAnglesFromInput(fileName, inputIndices, sensorIndex, True, allByTime, allByJoint)
    
    #normalizing  allByJoint
    timeSet = Set([])
    for inputIndices in featureSpaceIndices:
        vec = []
        for timeStamp in allByTime.keys():
            if(timeStamp in allByJoint[inputIndices].keys()):
                timeSet.add(timeStamp)
                x = allByJoint[inputIndices][timeStamp]
                vec.append(x)
        if(len(vec) > 0):
            vec = angleExtraction.normelizeVector(vec)
        i=0
        for timeStamp in  allByTime.keys():
            if(timeStamp in allByJoint[inputIndices].keys()):
                allByJoint[inputIndices][timeStamp] = vec[i]
                i = i + 1
    
    #time set to list, output dict to list 
    time = []
    for timeStamp in timeSet:
        time.append(timeStamp)
    time.sort()
    allOutput = []
    tmpTime = []
    #clean zeros, create time ordered output vector
    for timeStamp in time:
        out = allByTime[timeStamp]['output']
        if(out != 0 and len(allByTime[timeStamp]) == featureNum + 1):
            tmpTime.append(timeStamp)
            allOutput.append(out)
    time = tmpTime 

    #normalize allOutput
    allOutput = normalByPercentile(allOutput)
    #create a net
    hiddenSize = (featureNum + 2)/2
    net = buildNetwork(featureNum, hiddenSize, 1, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True, bias=True) 
    #build dataset
    ds = SequentialDataSet(featureNum, 1)
    i=0
    lastTimeStamp = time[0]
    for timeStamp in time:
        if(len(allByTime[timeStamp]) == featureNum+1):#it is a full vector
            if(timeStamp - lastTimeStamp > 100):
                ds.newSequence()
            sample = []
            for inputIndices in featureSpaceIndices:
                sample.append(allByTime[timeStamp][inputIndices])
            ds.appendLinked(sample, allOutput[i])
        i = i + 1
        lastTimeStamp = timeStamp
    #train
    net.randomize()
    tstdata, trndata = ds.splitWithProportion( 0.25 )
    trainer = BackpropTrainer(net, trndata)
    print len(ds)
    min = 100
    trainNum = 100
    bestTrainer = None
    for i in range(trainNum):
        res = trainer.train()
        if(res < min):
            min = res
            bestTrainer = trainer
        net.randomize()
    print min
    """
    res = 100
    while(res > min):
        net.randomize()
        res = trainer.train()
    """
    trainer = bestTrainer
    for i in range(trainNum):
        res = trainer.train()
        if(i % (trainNum/10) == 0):
            print res
    print 'trndata.evaluateModuleMSE ' + str(trndata.evaluateModuleMSE(net))
    print 'tstdata.evaluateModuleMSE ' + str(tstdata.evaluateModuleMSE(net))
    #print net.activateOnDataset(tstdata)
    hits = 0.0
    total = 0.0
    #res = net.activate(tstdata)
    for i in range(trndata.getNumSequences()):
        for input, target in trndata.getSequenceIterator(i):
            res = net.activate(input)
            total += 1
            if(res[0]*target[0] > 0):
                hits+=1        
    grade = hits / total
    print 'grade ' + str(grade)
    print 'total ' + str(total)
def train_network(options_file_location,training_data_location,output_location):

    training_file_handle = open(training_data_location,"r")
    training_reader = csv.reader(training_file_handle)
    
    stdout_file = output_location+'training_console_output.txt'
    stderr_file = output_location+'training_console_errput.txt'
    
    sys.stdout = open(stdout_file,"w")
    sys.stderr = open(stderr_file,"w")
    
    options_file_handle = open(options_file_location,'r')
    options_dictionary = {}

    for option in options_file_handle.readlines():
        key,val = option.split('=')
        print key
        print val
        options_dictionary[key] = val;

    num_predictors = int(options_dictionary['num_predictors'])
    num_outputs = int(options_dictionary['num_outputs'])
    num_training_epochs = int(options_dictionary['num_training_epochs'])
    num_hidden_neurons = int(options_dictionary['num_hidden_neurons'])
    compound_prediction = int(options_dictionary['compound_prediction'])
    #teacher_forced_transient = int(options_dictionary['teacher_forced_transient'])
    teacher_forced_transient = 0
    hidden_neuron_type_str = options_dictionary['hidden_neuron_type']
    output_neuron_type_str = options_dictionary['output_neuron_type']
    
    hidden_layer_type,output_layer_type = net_topol.get_layer_types(options_dictionary)
    
    training_dataset = SequentialDataSet(num_predictors, num_outputs)
    
    print 'reach'

    previous_sequence_number = 1
    
    #read data into dataset objects
    print 'reading in training data...'
    for row in training_reader:
        #convert list of strings to list of floats
        list = [float(s) for s in row]
        
        #split input line
        predictors = list[0:num_predictors]
        outputs = list[num_predictors+1:num_predictors+1+num_outputs]
        
        #convert from python list to numpy array
        predictors = np.array(predictors)
        outputs = np.array(outputs)
        
        sequence_number = math.trunc(list[num_predictors])
        
        if not sequence_number==previous_sequence_number:
            # print sequence_number
            # print previous_sequence_number
            training_dataset.newSequence()
        
        previous_sequence_number = sequence_number
        
        #add to dataset
        training_dataset.appendLinked(predictors, outputs)
        
    network = shortcuts.buildNetwork(num_predictors, num_hidden_neurons, num_outputs, hiddenclass=LSTMLayer, outclass=LinearLayer)
    network.sortModules();
    
    trainer = RPropMinusTrainer(module=network, dataset=training_dataset)

    for i in range(num_training_epochs):
        print 'Starting training epoch: '+str(i)
        trainer.trainEpochs(1)
        sys.stdout.flush()

    #brittle
    network_file_path = output_location+'trained_network.xml'
    NetworkWriter.writeToFile(network, network_file_path)
    done_file_handle = open(output_location+'training_done.txt',"w")
    done_file_handle.write('%s' % 'done!')
    done_file_handle.close()
def network_predict(options_file_location,prediction_data_location,output_location,network_location):

    prediction_data_file_handle = open(prediction_data_location,"r")
    prediction_data_reader = csv.reader(prediction_data_file_handle)
    
    stdout_file = output_location+'prediction_console_output.txt'
    stderr_file = output_location+'prediction_console_errput.txt'
    
    sys.stdout = open(stdout_file,"w")
    sys.stderr = open(stderr_file,"w")
    
    prediction_results_file_location = output_location+'prediction_results.csv'
    prediction_results_file_handle = open(prediction_results_file_location,"w")
    
    options_file_handle = open(options_file_location,'r')
    options_dictionary = {}

    for option in options_file_handle.readlines():
        key,val = option.split('=')
        print key
        print val
        options_dictionary[key] = val;

    num_predictors = int(options_dictionary['num_predictors'])
    num_outputs = int(options_dictionary['num_outputs'])
    num_training_epochs = int(options_dictionary['num_training_epochs'])
    num_hidden_neurons = int(options_dictionary['num_hidden_neurons'])
    compound_prediction = int(options_dictionary['compound_prediction'])
    # teacher_forced_transient = int(options_dictionary['teacher_forced_transient'])
    teacher_forced_transient = 0
    
    prediction_dataset = SequentialDataSet(num_predictors, num_outputs)
    
    previous_sequence_number = 1
    print 'reading in prediction data...'
    for row in prediction_data_reader:
        #convert list of strings to list of floats
        list = [float(s) for s in row]
        
        #split input line
        predictors = list[0:num_predictors]
        #+1 is to skip over the sequence column
        outputs = list[num_predictors+1:num_predictors+1+num_outputs]

        
        #convert from python list to numpy array
        predictors = np.array(predictors)
        outputs = np.array(outputs)
        
        sequence_number = math.trunc(list[num_predictors])
        
        if not sequence_number==previous_sequence_number:
            # print 'sequence_number '+str(sequence_number)
            # print 'previous_sequence_number '+str(previous_sequence_number)
            # frame_number_debug = 0;
            prediction_dataset.newSequence()
        
        previous_sequence_number = sequence_number
        
        #add to dataset
        prediction_dataset.appendLinked(predictors, outputs)
    
    network = NetworkReader.readFrom(network_location)
    
    if compound_prediction==0:
        results, targets, mse = evalRNN.evalRNNOnSeqDataset(network,prediction_dataset)
    elif compound_prediction==1:
        results, targets, mse = evalRNN.compoundEvalRNNOnSeqDataset(network,prediction_dataset, teacher_forced_transient)
                
    results_length, results_width = results.shape
 
    np.savetxt(prediction_results_file_location,results,delimiter=" ",fmt='%9.9f')
    
    done_file_handle = open(output_location+'predicting_done.txt',"w")
    done_file_handle.write('%s' % 'done!')
    done_file_handle.close()