def __init__(self, path="inputs/datasets/cifar10/", image_format="Grayscale", IMG_SIZE=32*32, LABELS_COUNT=10, IMAGES_PER_FILE=10000, FILES_AMOUNT=5, one_pixel=False): self.fileID=0 self.format = image_format self.IMG_SIZE = IMG_SIZE self.limiter = IMG_SIZE self.LABELS_COUNT = LABELS_COUNT self.IMAGES_PER_FILE = IMAGES_PER_FILE self.FILES_AMOUNT = FILES_AMOUNT self.data = [] self.testData = {} self.loadAllFiles(path) self.converter = SoundConverter("") self.one_pixel=one_pixel
def __init__(self, path="inputs/datasets/MNIST_data/", one_pixel=False): self.path = path self.LABELS_COUNT = 10 self.loadAllImages() self.cache = {} self.converter = SoundConverter("inputs/sounds/piano1.wav") self.converter.data = [0]
def __init__(self, LABELS_COUNT=1000, fixed_size=-1, sample_size=1 / 15, local=False, limit_genres=[], limit_track=-1, other_genres_rate=0.5, rating_labels=False, label_is_input=False, insert_global_input_state=False, encoding="None", extract_length=-1): self.LABELS_COUNT = LABELS_COUNT self.sample_size = sample_size self.fixed_size = fixed_size self.picker = SoundPicker(limit_genres=limit_genres, other_genres_rate=other_genres_rate) self.limit_genres = limit_genres self.other_genres_rate = other_genres_rate self.shuffle_rate = 12 self.rating_labels = rating_labels self.limit_track = limit_track self.label_is_input = label_is_input self.insert_global_input_state = insert_global_input_state self.encoding = encoding self.extract_length = extract_length if rating_labels or label_is_input: self.LABELS_COUNT = LABELS_COUNT elif len(limit_genres) != 0: self.LABELS_COUNT = len(limit_genres) if other_genres_rate != 0: self.LABELS_COUNT += 1 self.local = local track = self.picker.getRandomTrack(local=local) converter = SoundConverter(track["track"]) if fixed_size <= 0: self.image_width = int( np.sqrt( len( converter.ExtractRandomSample(sample_size=sample_size, multiplier=256)))) else: self.image_width = int(np.sqrt(fixed_size))
class CifarLoader: def __init__(self, path="inputs/datasets/cifar10/", image_format="Grayscale", IMG_SIZE=32*32, LABELS_COUNT=10, IMAGES_PER_FILE=10000, FILES_AMOUNT=5, one_pixel=False): self.fileID=0 self.format = image_format self.IMG_SIZE = IMG_SIZE self.limiter = IMG_SIZE self.LABELS_COUNT = LABELS_COUNT self.IMAGES_PER_FILE = IMAGES_PER_FILE self.FILES_AMOUNT = FILES_AMOUNT self.data = [] self.testData = {} self.loadAllFiles(path) self.converter = SoundConverter("") self.one_pixel=one_pixel def loadAllFiles(self, path): print("Loading files...") if self.FILES_AMOUNT<=1: self.loadFile(path+"train", False) self.loadFile(path+"test", True) else: for i in range(self.FILES_AMOUNT): file_name = path+"data_batch_"+str(i+1) self.loadFile(file_name) file_name = path+"test_batch" self.loadFile(path+"test_batch", True) def loadFile(self, file, isTest=False): fo = open(file, 'rb') u = pickle._Unpickler(fo) u.encoding = 'latin1' dict = u.load() fo.close() dict["training_state"] = 0 print("Loaded: "+file) if isTest==False: self.data.append(dict) else: self.testData = dict """ getNextBatch(int amount, int fileID=0): int[] image = new int[IMG_SIZE] int[] label = new int[LABELS_COUNT] return [imagesData List<image>, labels List<label>] """ def getNextBatch(self, amount): imagesData = [] labels = [] flipped = [] for i in range(amount): fileID = 0 imgID = 0 if self.FILES_AMOUNT<=1: fileID = 1 else: fileID = randrange(1, self.FILES_AMOUNT) imgID = randrange(0, self.IMAGES_PER_FILE-1) imgObj = self.loadOneImage(imgID, fileID) imagesData.append(imgObj["image"]) labels.append(imgObj["label"]) flipped.append(imgObj["flipped"]) return [imagesData, labels, flipped] def getNextTimeBatch(self, amount, n_steps=32): imagesData = [] labels = [] flipped = [] for i in range(amount): fileID = 0 imgID = 0 if self.FILES_AMOUNT<=1: fileID = 1 else: fileID = randrange(1, self.FILES_AMOUNT) imgID = randrange(0, self.IMAGES_PER_FILE-1) imgObj = self.loadOneImage(imgID, fileID) imgObj["image"] = self.converter.reshapeAsSequence(imgObj["image"], n_steps) imagesData.append(imgObj["image"]) labels.append(imgObj["label"]) flipped.append(imgObj["flipped"]) return [imagesData, labels, flipped] def getTestBatch(self, amount): imagesData = [] labels = [] for i in range(amount): imgID = randrange(0, amount) imgObj = self.loadOneTestImage(imgID) imagesData.append(imgObj["image"]) labels.append(imgObj["label"]) return [imagesData, labels] def getImageData(self, index, fileID=0): return self.data[fileID]["data"][index] def loadOneTestImage(self, index): _image = [] _label = None if self.one_pixel: if self.format=="Grayscale": raise Exception("Grayscale for generation not implemented") for byte in range(self.IMG_SIZE*3-1): pixel = self.testData["data"][index][byte] _image.append(pixel/255) _label = [0]*256 _label[self.testData["labels"][index]] = 1 else: if self.format=="Grayscale": for byte in range(self.IMG_SIZE): pixel = (self.testData["data"][index][byte]+self.testData["data"][index][self.IMG_SIZE+byte]+self.testData["data"][index][self.IMG_SIZE*2+byte])/3 _image.append(pixel/255) else: for byte in range(self.IMG_SIZE*3): pixel = self.testData["data"][index][byte] _image.append(pixel/255) _label = [0]*10 _label[self.testData["labels"][index]] = 1 return {"image":_image, "label":_label} def loadOneImage(self, index, fileID=0): _image = [] flipped=rand.choice([0, 2]); staturation = rand.uniform(-10.0, 10.0) if self.format=="Grayscale": for byte in range(self.IMG_SIZE): if flipped: pixel = (self.data[fileID]["data"][index][self.IMG_SIZE-byte-1]+self.data[fileID]["data"][index][self.IMG_SIZE+byte]+self.data[fileID]["data"][index][self.IMG_SIZE*3-byte-1])/3 else: pixel = (self.data[fileID]["data"][index][byte]+self.data[fileID]["data"][index][self.IMG_SIZE+byte]+self.data[fileID]["data"][index][self.IMG_SIZE*2+byte])/3 _image.append(max(0, min(255, pixel+staturation))/255) else: for byte in range(self.IMG_SIZE*3): if flipped==1: pixel = (self.data[fileID]["data"][index][self.IMG_SIZE-byte-1]) elif flipped==2: color_channel = int(byte/(32*32))*32*32 local = byte-color_channel x = 32-local%32-1 y = 32-int(local/32) pixel = (self.data[fileID]["data"][index][color_channel+local]) else: pixel = self.data[fileID]["data"][index][byte] _image.append((pixel+staturation)/255) _label = [0]*10 _label[self.data[fileID]["labels"][index]] = 1 return {"image":_image, "label":_label, "flipped": flipped} def getImageBytes(self): if self.format=="Grayscale": return self.IMG_SIZE else: return self.IMG_SIZE*3 def getImageWidth(self): if self.format=="Grayscale": return int(np.sqrt(self.IMG_SIZE)) else: return int(np.sqrt(self.IMG_SIZE*3)) def getImagesCount(self): return self.IMAGES_PER_FILE*self.FILES_AMOUNT def getTrainedCount(self): count = 0 for i in range(self.FILES_AMOUNT): count+= self.data[i]["training_state"] return count def rangeFactor(t, point, _range): ratio = np.abs (point - t) / _range; if ratio < 1: return 1 - ratio; else: return 0;
def getNextTimeBatch(self, batch_size, shuffle_rate=-1, n_steps=32): images = [] labels = [] track = None converters = {} converter = None match = 0 if shuffle_rate == -1: shuffle_rate = self.shuffle_rate i = 0 rank_counter = 1 while len(images) < batch_size: if self.rating_labels: track = self.picker.getRandomTrackOfRankAndGenre( rank_counter, self.limit_genres[0]) rank_counter += 1 if converters.__contains__(track["id"]): converter = converters[track["id"]] else: converter = SoundConverter(track["track"]) converters[track["id"]] = converter if rank_counter > 9: rank_counter = 1 else: if i % shuffle_rate == 0 or i == 0: if self.limit_track == -1: track = self.picker.getRandomTrack(local=self.local) else: track = self.picker.tracks[self.limit_track] if converters.__contains__(track["id"]): converter = converters[track["id"]] else: converter = SoundConverter(track["track"]) converters[track["id"]] = converter #print("loaded track:"+str(track["id"])+" label: "+str(track["genre"])) limiter = self.image_width * self.image_width if self.fixed_size > 0: limiter = self.fixed_size image = converter.ExtractRandomSample(fixed_size=self.fixed_size, sample_size=self.sample_size, multiplier=128, limiter=limiter, offset=128) limiter = self.image_width if self.fixed_size > 0: limiter = int(np.sqrt(self.fixed_size)) if self.label_is_input: label = image[len(image) - self.extract_length:len(image)] if self.encoding == "OneHot": label = Encoder.OneHot(label, self.LABELS_COUNT) labels.append(label) image = converter.reshapeAsSequence( image[0:len(image) - self.extract_length], n_steps) if self.insert_global_input_state: image.append([converter.last_sample_position] * len(image[0])) images.append(image) else: image = converter.reshapeAsSequence(image, n_steps) if self.insert_global_input_state: image.append([converter.last_sample_position] * len(image[0])) if len(image) != limiter: raise Exception("sample length was: " + str(len(image)) + " expected: " + str(limiter)) label = [0] * self.LABELS_COUNT if self.encoding == "OneHot": label = Encoder.OneHot(label, self.LABELS_COUNT) if self.rating_labels: label = [0, 0] if track.__contains__("rank") and track["rank"] >= 0: label[0] = track["rank"] / 1000000 label[1] = 1 - track["rank"] / 1000000 images.append(image) labels.append(label) match += 1 else: i -= 1 elif len(self.limit_genres) == 0: label[track["genre"]] = 1 images.append(image) labels.append(label) else: if self.limit_genres.__contains__(track["genre"]): label[self.limit_genres.index(track["genre"])] = 1 match += 1 else: label[len(self.limit_genres)] = 1 images.append(image) labels.append(label) print("extracted " + str(len(images)) + " samples from " + str(int(batch_size / self.shuffle_rate)) + " tracks (matches: " + str(match) + ")") return [images, labels]
def getTestBatch(self, batch_size): images = [] labels = [] track = None converters = {} converter = None match = 0 track = self.picker.getRandomTrack(local=self.local) print("testing track: " + str(track)) self.lastTrack = track if converters.__contains__(track["id"]): converter = converters[track["id"]] else: converter = SoundConverter(track["track"]) converters[track["id"]] = converter for i in range(batch_size): #print("loaded track:"+str(track["id"])+" label: "+str(track["genre"])) image = converter.ExtractRandomSample(fixed_size=self.fixed_size, sample_size=self.sample_size, multiplier=256, limiter=self.image_width * self.image_width) if len(image) != self.image_width * self.image_width: raise Exception("sample length was: " + str(len(image)) + " expected: " + str(self.image_width * self.image_width)) label = [0] * self.LABELS_COUNT if self.rating_labels: label = [0, 0] if track.__contains__("rank") and track["rank"] >= 0: label[0] = track["rank"] / 1000000 label[1] = 1 - track["rank"] / 1000000 images.append(image) labels.append(label) match += 1 else: i -= 1 elif len(self.limit_genres) == 0: label[track["genre"]] = 1 images.append(image) labels.append(label) else: if self.limit_genres.__contains__(track["genre"]): label[self.limit_genres.index(track["genre"])] = 1 match += 1 else: label[len(self.limit_genres)] = 1 images.append(image) labels.append(label) print("extracted " + str(len(images)) + " samples from " + str(batch_size / self.shuffle_rate) + " tracks (matches: " + str(match) + ")") return [images, labels]