TorchDSP / torchsig

TorchSig is an open-source signal processing machine learning toolkit based on the PyTorch data handling pipeline.
MIT License
170 stars 38 forks source link

Include transform to change spectrograms into images. #160

Open jcr0269 opened 1 year ago

jcr0269 commented 1 year ago

Would love a way to save the datasets as .png files or other file types. I have run into a few issues when trying to decode the lmdb file and pull out the data instead of just passing it into torch. Such as by trying to unpickle the folder like: ############################################################################# def extract_images(lmdb_path, output_dir): env = lmdb.open(lmdb_path, readonly=True)

if not os.path.exists(output_dir):
    os.makedirs(output_dir)

with env.begin() as txn:
    cursor = txn.cursor()
    for i, (key, value) in enumerate(cursor):
        key = int.from_bytes(key, 'big')  # Convert from bytes to int
        try:
            img_data = pickle.loads(value)  # Convert from bytes to data
            # Convert byte data to PIL Image
            img = Image.open(io.BytesIO(img_data))

            img.save(os.path.join(output_dir, f'{key}.jpg'))

        except Exception as e:
            print(f'Error processing key {key}: {str(e)}')
            print(value)
        # Stop after checking 5 images
        if i >= 5:
            break

############################################################################## If there is an easier way to do this though, please let me know.

gvanhoy commented 1 year ago

I think the best way here is to have a ToPNG or ToJPEG transform that can be used in tandem with the DatasetCreator. You just add this transform at the end of the transforms pipeline and then hand the dataset over to the DatasetCreator.

On the training/reading-from-disk end, you'd have to have a FromPNG or FromJPEG transform when you read it from the disk. I think this is the best because then we can keep WidebandSig53 as it is and just add a few transforms to the codebase.

jcr0269 commented 1 year ago

awe okay, ill spend some time today working on that and try to post a solution if I get one working. Thank you for the quick reply

gvanhoy commented 1 year ago

Related to #181 in a way.