Jeremy26 / segmentation_course

Semantic Segmentation from https://jeremycohen.podia.com/image-segmentation
4 stars 5 forks source link

Can you share the script for the pickle file conversion of 3000 Label dataset #1

Open imharshanand opened 3 years ago

imharshanand commented 3 years ago

import numpy as np import pickle import matplotlib.image as mpimg

import cv2 import os import glob

img_dir = os.getcwd()+"/postmortem/" #Enter Directory of all images data_path = os.path.join(img_dir, '*g') files = glob.glob(data_path) files.sort() data = [] print(len(files))

width = 160 #int(1280 scale_percent / 100) height = 80 #int(720 scale_percent / 100) dsize = (width, height)

for f1 in files: img = mpimg.imread(f1) img = cv2.resize(img, dsize) data.append(img)

file = open('pickle_label.p', 'wb') pickle.dump(data, file) file.close()

I used this script but the pickle file which I got has all the pixel values, your label pickle file has pixel value [0, 0.5, 1]

Jeremy26 commented 3 years ago

Sure, here's my script!

import numpy as np 
import pickle
import matplotlib.image as mpimg

import cv2
import os
import glob

img_dir = os.getcwd()+"/drivable_labels" # Enter Directory of all images 
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
files.sort()
data = []
print(len(files))
scale_percent = 25

width = 160 #int(1280 * scale_percent / 100)
height = 80 #int(720 * scale_percent / 100)
dsize = (width, height)

for i in range(1500, len(files)):
    img = mpimg.imread(files[i])
    #img = cv2.resize(img, dsize)
    data.append(img)

print(np.array(data).shape)

class MacOSFile(object):

    def __init__(self, f):
        self.f = f

    def __getattr__(self, item):
        return getattr(self.f, item)

    def read(self, n):
        # print("reading total_bytes=%s" % n, flush=True)
        if n >= (1 << 31):
            buffer = bytearray(n)
            idx = 0
            while idx < n:
                batch_size = min(n - idx, 1 << 31 - 1)
                # print("reading bytes [%s,%s)..." % (idx, idx + batch_size), end="", flush=True)
                buffer[idx:idx + batch_size] = self.f.read(batch_size)
                # print("done.", flush=True)
                idx += batch_size
            return buffer
        return self.f.read(n)

    def write(self, buffer):
        n = len(buffer)
        print("writing total_bytes=%s..." % n, flush=True)
        idx = 0
        while idx < n:
            batch_size = min(n - idx, 1 << 31 - 1)
            print("writing bytes [%s, %s)... " % (idx, idx + batch_size), end="", flush=True)
            self.f.write(buffer[idx:idx + batch_size])
            print("done.", flush=True)
            idx += batch_size

def pickle_dump(obj, file_path):
    with open(file_path, "wb") as f:
        return pickle.dump(obj, MacOSFile(f), protocol=pickle.HIGHEST_PROTOCOL)

"""
def pickle_load(file_path):
    with open(file_path, "rb") as f:
        return pickle.load(MacOSFile(f))
"""
pickle_dump(data, "labels_3000_original_2.p")

"""
e = pickle_load("labels_2_classes.p")

import matplotlib
matplotlib.use('PS')
print(e[0].shape)
#import matplotlib.pyplot as plt
#plt.imshow(e[0])
#plt.show()
"""
imharshanand commented 3 years ago

Issue 1: protocol=pickle.HIGHEST_PROTOCOL was not working but changing this to protocol=4 worked, and I was able to generate the pickle file. Issue 2: Your script generated a pickle file which had all the pixel values. But the pickle file you provided for labels comprises of only [0, 0.5, 1]. Here is a screenshot of all my issues regarding the label pickle file. https://imgur.com/gallery/71CkmAS Screenshot 2021-04-28 152804 Screenshot 2021-04-28 153527 Screenshot 2021-04-28 153315