def sample2(prefix, cliptrain, clipvalid): namestrain, namesvalid = [], [] indextrain, indexvalid = [], [] labelstrain, labelsvalid = [], [] attrstrain, attrsvalid = [], [] for i in range(20): path = utilities.getPath(prefix + '_' + str(i + 1) + '.cpk') obj = utilities.loadObject(path) names, index, labels, attrs = obj[0], obj[1], obj[2], obj[3] for idx in index: j = idx - 1 - i * 500 if j in cliptrain: # print 'train: ', idx, j namestrain.append(names[j]) indextrain.append(index[j]) labelstrain.append(labels[j]) attrstrain.append(attrs[j]) elif j in clipvalid: # print 'valid: ', idx, j namesvalid.append(names[j]) indexvalid.append(index[j]) labelsvalid.append(labels[j]) attrsvalid.append(attrs[j]) else: pass datatrain = [namestrain, indextrain, labelstrain, attrstrain] datavalid = [namesvalid, indexvalid, labelsvalid, attrsvalid] fntrain = utilities.getPath('train_2000_samples.cpk') fnvalid = utilities.getPath('valid_1000_samples.cpk') utilities.saveObjects([datatrain, datavalid], [fntrain, fnvalid])
def sample(prefix, nprefix, clip, num=20): namesset = [] indexset = [] lablesset = [] attrsset = [] for i in range(num): path1 = utilities.getPath(prefix + '_' + str(i + 1) + '.cpk') obj = utilities.loadObject(path1) names, index, labels, attrs = obj[0], obj[1], obj[2], obj[3] for idx in index: j = idx - 1 - i * 500 if j in clip: # print nprefix, idx, j namesset.append(names[j]) indexset.append(index[j]) lablesset.append(labels[j]) attrsset.append(attrs[j]) else: pass dataset = [namesset, indexset, lablesset, attrsset] fn2 = nprefix + '_' + str(len(clip)) + '_' + 'samples.cpk' path2 = utilities.getPath(fn2) utilities.saveObject(dataset, path2)
def pre_define_processor(mode=''): if mode == 'train' or mode == 'valid': path = utilities.getPath('train.txt') s1, s2 = 5, 6 post = '' elif mode == 'testA': path = utilities.getPath('testA.txt') s1, s2 = 4, 6 post = 'A' else: raise Exception('Value Empty Error! ') return path, s1, s2, post
def smaller(prefix, m, n): for i in range(m): fn1 = prefix + '_' + str(i + 1) + '.cpk' path = utilities.getPath(fn1) names, index, labels, attrs = utilities.loadObject(path) for j in range(n): nms = names[j * 100:(j + 1) * 100] idx = index[j * 100:(j + 1) * 100] lbs = labels[j * 100:(j + 1) * 100] ats = attrs[j * 100:(j + 1) * 100] fn2 = prefix + '_' + 's' + '_ ' + str(i * 5 + (j + 1)) + '.cpk' path = utilities.getPath(fn2) object = [nms, idx, lbs, ats] utilities.saveObject(object, path)
def merge(prefix, num): labels = [] attrs = [] for i in xrange(num): fn = prefix + '_' + str(i + 1) + '.cpk' path = utilities.getPath(fn) object = loadObject(path) lbs, ats = object[2], object[3] labels += lbs attrs += ats labelsM = numpy.mat(labels) attrsM = numpy.mat(attrs) obj = [labelsM, attrsM] path = utilities.getPath(prefix + '.cpk') saveObjects([obj], [path])
def load(fn1, s1, s2, post, size): names = [] index = [] labels = [] attrs = [] with open(fn1, 'r') as rf: for lineFile in rf.readlines(): strLine = lineFile.strip().split(',') info, label, attr = strLine[0], strLine[1], strLine[2] sname, sidx = info[:s1], info[s2:] curattr = attr.strip().split() try: map(float, curattr) except Exception: print utilities.fetchTime( ) + " ValueError: could not convert string to float: ", curattr else: vname = sname + post names.append(vname) idx = int(sidx) # curinfo = [idx] index.append(idx) fltlabel = float(label) # vfltlabel = [fltlabel] labels.append(fltlabel) fltattr = map(float, curattr) attrs.append(fltattr) # print count '''''' if (idx % size == 0): dataset = [names, index, labels, attrs] # for item in dataset: # print utilities.fetchTime() + ' ', len(item), len(item[0]) '''''' iter = idx / size fn2 = utilities.getPath(vname + '_' + str(iter) + '.cpk') saveObjects([dataset], [fn2]) names = [] index = [] labels = [] attrs = [] assert (len(names) or len(index) or len(labels) or len(attrs)) == 0
def export(self, name): success = True try: path = os.path.join(util.getPath(), str(name) + ".png") image = Image.new("RGBA", (self.width, self.height), "black") pixels = image.load() for x in range(self.width): for y in range(self.height): pixel = self.units[x][y].getElements() for i in range(len(pixel)): pixel[i] = util.getClamp(int(pixel[i]), 0, 255) pixels[x, y] = tuple(pixel) image.save(path) except: traceback.print_exc() success = False return success
def loadObject(filename): object = [] with open(filename, 'r') as rf: names, index, labels, attrs = cPickle.load(rf) object += [names, index, labelst, attrs] print str( datetime.datetime.now())[:19] + " " + filename + " is loaded! " return object def loadObjects(filenames): objects = [] for fn in filenames: with open(fn, 'r') as rf: names, index, labels, attrs = cPickle.load(rf) obj = [names, index, labels, attrs] objects.append(obj) print str( datetime.datetime.now())[:19] + " " + filename + " is loaded! " return objects if __name__ == '__main__': fn1 = utilities.getPath('testA.txt') fn2 = utilities.getPath('samples_testA.txt') load(fn2, 2) else: print str(datetime.datetime.now())[:19] + ' importing module loader ... ' # end