sunana commited on
Commit
ca12b2c
1 Parent(s): a5423cb

Upload V1.py

Browse files
Files changed (1) hide show
  1. V1.py +0 -164
V1.py CHANGED
@@ -7,7 +7,6 @@ from torch import nn
7
  from torch.nn import functional as F
8
  import matplotlib.pyplot as plt
9
  import os
10
- import pandas as pd
11
  import imageio
12
  from torch.cuda.amp import autocast as autocast
13
 
@@ -744,166 +743,3 @@ def circular_hist(ax, x, bins=16, density=True, offset=0, gaps=True):
744
  ax.set_yticks([])
745
 
746
  return n, bins, patches
747
-
748
-
749
- def show_trained_model(file_name="/home/2TSSD/experiment/FFMEDNN/Sintel_fixv1_10.62_ckpt.pth.tar"):
750
- import utils.torch_utils as utils
751
- from model.fle_version_2_3.FFV1MT_MS import FFV1DNN
752
- model = FFV1DNN(num_scales=8,
753
- num_cells=256,
754
- upsample_factor=8,
755
- feature_channels=256,
756
- scale_factor=16,
757
- num_layers=6)
758
- # model = utils.restore_model(model, file_name)
759
- model = model.ffv1
760
- t_point = 100
761
- s_point = 100
762
- t_kz = 6
763
- filenames = []
764
- x = np.arange(0, 6) * 40
765
- x = np.repeat(x[None], axis=0, repeats=256)
766
- temporal = model.temporal_pooling.data.cpu().squeeze().numpy()
767
- mean = np.mean(temporal, axis=0)
768
- plt.figure(figsize=(10, 10))
769
- plt.subplot(2, 1, 1)
770
- for idx in range(0, 256):
771
- plt.plot(x[idx], temporal[idx])
772
- plt.subplot(2, 1, 2)
773
- plt.plot(x[0], mean, label="mean")
774
-
775
- plt.xlabel("times (ms)")
776
- plt.ylabel("temporal pooling weight")
777
- plt.legend()
778
- plt.grid(True)
779
- plt.show()
780
- neural_representation = model._get_v1_order()
781
-
782
- fs = np.array([ne["fs"] for ne in neural_representation])
783
- ft = np.array([ne["ft"] for ne in neural_representation])
784
-
785
- ax1 = plt.subplot(131, projection='polar')
786
- theta_list = []
787
- v_list = []
788
- energy_list = []
789
- for index in range(len(neural_representation)):
790
- v = neural_representation[index]["speed"]
791
- theta = neural_representation[index]["theta"]
792
- theta_list.append(theta)
793
- v_list.append(v)
794
-
795
- v_list, theta_list = np.array(v_list), np.array(theta_list)
796
- x, y = pol2cart(v_list, theta_list)
797
- plt.scatter(theta_list, v_list, c=v_list, cmap="rainbow", s=(v_list + 20), alpha=0.8)
798
- plt.axis('on')
799
- # plt.colorbar()
800
- plt.grid(True)
801
- # plt.subplot(132, projection="polar")
802
- # plt.scatter(theta_list, np.ones_like(theta_list))
803
- plt.subplot(132, projection='polar')
804
- plt.scatter(theta_list, np.ones_like(v_list))
805
- lst = []
806
- for scale in range(8):
807
- lst += ["scale %d" % scale] * 32
808
- data = {"Spatial Frequency": fs, 'Temporal Frequency': ft, "Class": lst}
809
- df = pd.DataFrame(data=data)
810
- ax = plt.subplot(133, projection='polar')
811
- # theta_list = theta_list[v_list > (ft * v_list.mean())]
812
- print(len(theta_list))
813
- bins_number = 8 # the [0, 360) interval will be subdivided into this
814
- # number of equal bins
815
- zone = np.pi / 8
816
- theta_list[theta_list < (-np.pi + zone)] = theta_list[theta_list < (-np.pi + zone)] + np.pi * 2
817
- bins = np.linspace(-np.pi + zone, np.pi + zone, bins_number + 1)
818
- n, _, _ = plt.hist(theta_list, bins, edgecolor="black")
819
- # ax.set_theta_offset(-np.pi / 8 - np.pi)
820
- ax.set_yticklabels([])
821
- plt.grid(True)
822
- import seaborn as sns
823
- sns.jointplot(data=df, x="Spatial Frequency", y="Temporal Frequency", hue="Class", xlim=[0, 0.3], ylim=[0, 0.3])
824
- plt.grid(True)
825
- g = sns.jointplot(data=df, x="Spatial Frequency", y="Temporal Frequency", xlim=[0, 0.25], ylim=[0, 0.25])
826
- # g.plot_joint(sns.kdeplot, color="r", zorder=0, levels=6)
827
-
828
- plt.grid(True)
829
- plt.show()
830
-
831
- # show spatial frequency preference and temporal frequency preference.
832
-
833
- x = np.linspace(0, t_kz, t_point)
834
- index = 0
835
- for scale in range(len(model.spatial_filter)):
836
- t_sin, t_cos = model.temporal_filter[scale].demo_temporal_filter(points=t_point)
837
- gb_sin_b, gb_cos_b = model.spatial_filter[scale].demo_gabor_filters(points=s_point)
838
- for i in range(gb_sin_b.size(0)):
839
- plt.figure(figsize=(14, 9), dpi=80)
840
- plt.subplot(2, 3, 1)
841
- curve = gb_sin_b[i].squeeze().detach().numpy()
842
- plt.imshow(curve)
843
- plt.title("Gabor Sin")
844
- plt.subplot(2, 3, 2)
845
- curve = gb_cos_b[i].squeeze().detach().numpy()
846
- plt.imshow(curve)
847
- plt.title("Gabor Cos")
848
-
849
- plt.subplot(2, 3, 3)
850
- curve = t_sin[i].squeeze().detach().numpy()
851
- plt.plot(x, curve, label='sin')
852
- plt.title("Temporal Sin")
853
-
854
- curve = t_cos[i].squeeze().detach().numpy()
855
- plt.plot(x, curve, label='cos')
856
- plt.xlabel('Time (s)')
857
- plt.ylabel('Response to pulse at t=0')
858
- plt.legend()
859
- plt.title("Temporal filter")
860
-
861
- gb_sin = gb_sin_b[i].squeeze().detach()[5, :]
862
- gb_cos = gb_cos_b[i].squeeze().detach()[5, :]
863
-
864
- a = np.outer(t_cos[i].detach(), gb_sin)
865
- b = np.outer(t_sin[i].detach(), gb_cos)
866
- g_o = a + b
867
-
868
- a = np.outer(t_sin[i].detach(), gb_sin)
869
- b = np.outer(t_cos[i].detach(), gb_cos)
870
- g_e = a - b
871
- energy_component = g_o ** 2 + g_e ** 2
872
-
873
- plt.subplot(2, 3, 4)
874
- curve = g_o
875
- plt.imshow(curve, cmap="gray")
876
- plt.title("Spatial Temporal even")
877
- plt.subplot(2, 3, 5)
878
- curve = g_e
879
- plt.imshow(curve, cmap="gray")
880
- plt.title("Spatial Temporal odd")
881
-
882
- plt.subplot(2, 3, 6)
883
- curve = energy_component
884
- plt.imshow(curve, cmap="gray")
885
- plt.title("energy")
886
- plt.savefig('filter_%d.png' % (index))
887
- filenames.append('filter_%d.png' % (index))
888
- index += 1
889
- # plt.show()
890
-
891
- # build gif
892
- with imageio.get_writer('filters_orientation.gif', mode='I') as writer:
893
- for filename in filenames:
894
- image = imageio.imread(filename)
895
- writer.append_data(image)
896
-
897
- # Remove files
898
- for filename in set(filenames):
899
- os.remove(filename)
900
-
901
-
902
- if __name__ == "__main__":
903
- show_trained_model()
904
- # V1.demo()
905
- # draw_polar()
906
- # # V1.demo()
907
- # # draw_polar()
908
- show_trained_model()
909
- # te_spatial_temporal()
 
7
  from torch.nn import functional as F
8
  import matplotlib.pyplot as plt
9
  import os
 
10
  import imageio
11
  from torch.cuda.amp import autocast as autocast
12
 
 
743
  ax.set_yticks([])
744
 
745
  return n, bins, patches