Python Forum
I'm trying to visualize neuron weights with PIL but getting a white image.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm trying to visualize neuron weights with PIL but getting a white image.
#1
from PIL import Image
import copy
import numpy as np
import pickle
#-------Sample--------------------------------------(THIS WORKS)
# value = "01101000011001010110110001101100011011111"

# print(len(value))

# cmap = {'0': (0,155,3,0),
#         '1': (0,0,0)}

# data = [cmap[letter] for letter in value]
# img = Image.new('RGB', (8, (len(value)//8)+1), "white")
# print(data)
# img.putdata(data)
# img.show() 

#-------Sample---------------------------------------


class para_viz:
    def __init__(self,layers):
        self.layers=copy.deepcopy(layers)
        self.map()
    
    def map(self):
        for i in range(len(self.layers)):
            self.layers[i]=self.layers[i]/np.max(np.absolute(self.layers[i]),axis=0)
            self.layers[i]=self.layers[i]*255   
    
    def color_map(self,data):
        Data=[]
        for i in data:
            i=int(i)
            color=(i,0,0) if i>=0 else (0,i,0)
            Data.append(color)
        return Data

    def viz_neuron(self):
        neuron=self.layers[0][:,0]
        # print(neuron)
        flat_len=len(neuron)
        size=(flat_len//2,flat_len//2) if flat_len%2==0 else (flat_len//2,(flat_len//2)+1)
        img=Image.new('RGB',size,"white")
        data=self.color_map(neuron)
        print(len(data))
        img.putdata(data)
        img.show()


with open("weights.pkl","rb") as f:
    weights=pickle.load(f)

viz=para_viz(weights)
viz.viz_neuron()
weights.pkl---->https://drive.google.com/file/d/1ayDFQCigFQqe5vWdaR0QyMWh8OxYRmNc/view?usp=sharing
Reply
#2
from PIL import Image
import copy
import numpy as np

class para_viz:
    def __init__(self, layers):
        self.layers = copy.deepcopy(layers)
        self.map()
     
    def map(self):
        for i in range(len(self.layers)):
            self.layers[i] = self.layers[i] / np.max(np.absolute(self.layers[i]), axis=0)
            self.layers[i] = self.layers[i] * 255   
     
    def color_map(self, data):
        Data = []
        for i in data:
            i = int(i)
            color = (i, 0, 0) if i >= 0 else (0, i, 0)
            Data.append(color)
        return Data

    def viz_neuron(self):
        neuron = self.layers[0][:, 0]
        flat_len = len(neuron)
        size = (flat_len // 2, flat_len // 2) if flat_len % 2 == 0 else (flat_len // 2, (flat_len // 2) + 1)
        img = Image.new('RGB', size, "white")
        data = self.color_map(neuron)
        img.putdata(data)
        img.show() #Link Removed

# Usage
viz = para_viz(weights)
viz.viz_neuron()
Gribouillis write May-31-2024, 08:19 AM:
Clickbait link removed. Please read What to NOT include in a post
Gribouillis write May-31-2024, 08:18 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] White edge sdgdfgg 4 1,961 Dec-14-2023, 04:48 AM
Last Post: reneejenkins
  What should I do the character comes to the door the background changes in to white. Killdoz 0 1,769 May-22-2020, 02:41 PM
Last Post: Killdoz
  Visual Studio 2019 and Neuron JamesBrown 0 2,396 Apr-06-2019, 05:12 PM
Last Post: JamesBrown
  Deleting White from Bitmap Anysja 8 8,304 Aug-14-2018, 04:04 PM
Last Post: Anysja
  [WxPython] How to create a static white box text giu88 2 3,359 Aug-14-2018, 10:50 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020