code:
chrome.extension.sendMessage({}, function(response) {
  var tc = {
    settings: {
      speed: 1.0,           // default 1x
      speedStep: 0.1,       // default 0.1x
      rewindTime: 10,       // default 10s
      advanceTime: 10,      // default 10s
      resetKeyCode:  82,    // default: R
      slowerKeyCode: 83,    // default: S
      fasterKeyCode: 68,    // default: D
      rewindKeyCode: 90,    // default: Z
      advanceKeyCode: 88,   // default: X
      displayKeyCode: 86,   // default: V
      rememberSpeed: true, // default: true
      blacklist: `
      `.replace(/^\s+|\s+$/gm,'')
      /*
        www.instagram.com
        twitter.com
        vine.co
        imgur.com
       */
    }
  };

  chrome.storage.sync.get(tc.settings, function(storage) {
    tc.settings.speed = Number(storage.speed);
    tc.settings.speedStep = Number(storage.speedStep);
    tc.settings.rewindTime = Number(storage.rewindTime);
    tc.settings.advanceTime = Number(storage.advanceTime);
    tc.settings.resetKeyCode = Number(storage.resetKeyCode);
    tc.settings.rewindKeyCode = Number(storage.rewindKeyCode);
    tc.settings.slowerKeyCode = Number(storage.slowerKeyCode);
    tc.settings.fasterKeyCode = Number(storage.fasterKeyCode);
    tc.settings.displayKeyCode = Number(storage.displayKeyCode);
    tc.settings.advanceKeyCode = Number(storage.advanceKeyCode);
    tc.settings.rememberSpeed = Boolean(storage.rememberSpeed);
    tc.settings.blacklist = String(storage.blacklist);

    initializeWhenReady(document);
  });

  var forEach = Array.prototype.forEach;

  function defineVideoController() {
    tc.videoController = function(target, parent) {
      this.video = target;
      this.parent = target.parentElement || parent;
      this.document = target.ownerDocument;
      this.id = Math.random().toString(36).substr(2, 9);
      if (!tc.settings.rememberSpeed) {
        tc.settings.speed = 1.0;
      }
      this.initializeControls();

      target.addEventListener('play', function(event) {
        target.playbackRate = tc.settings.speed;
      });

      target.addEventListener('ratechange', function(event) {
        if (target.readyState === 0) {
          return;
        }
        var speed = this.getSpeed();
        this.speedIndicator.textContent = speed;
        tc.settings.speed = speed;
        chrome.storage.sync.set({'speed': speed});
      }.bind(this));

      target.playbackRate = tc.settings.speed;
    };

    tc.videoController.prototype.getSpeed = function() {
      return parseFloat(this.video.playbackRate).toFixed(2);
    }

    tc.videoController.prototype.remove = function() {
      this.parentElement.removeChild(this);
    }

    tc.videoController.prototype.initializeControls = function() {
      var document = this.document;
      var speed = parseFloat(tc.settings.speed).toFixed(2),
        top = Math.max(this.video.offsetTop, 0) + "px",
        left = Math.max(this.video.offsetLeft, 0) + "px";

      var prevent = function(e) {
        e.preventDefault();
        e.stopPropagation();
      }

      var wrapper = document.createElement('div');
      wrapper.classList.add('vsc-controller');
      wrapper.dataset['vscid'] = this.id;
      wrapper.addEventListener('dblclick', prevent, true);
      wrapper.addEventListener('mousedown', prevent, true);
      wrapper.addEventListener('click', prevent, true);

      var shadow = wrapper.createShadowRoot();
      var shadowTemplate = `

          ${speed}

            <button data-action="rewind" class="rw">«</button>
            <button data-action="slower">-</button>
            <button data-action="faster">+</button>
            <button data-action="advance" class="rw">»</button>
            <button data-action="close" class="hideButton">x</button>

      `;
      shadow.innerHTML = shadowTemplate;
      shadow.querySelector('.draggable').addEventListener('mousedown', (e) => {
        runAction(e.target.dataset['action'], document);
      });

      forEach.call(shadow.querySelectorAll('button'), function(button) {
        button.onclick = (e) => {
          runAction(e.target.dataset['action'], document);
        }
      });

      this.speedIndicator = shadow.querySelector('span');
      var fragment = document.createDocumentFragment();
      fragment.appendChild(wrapper);

      // Note: when triggered via a MutationRecord, it's possible that the
      // target is not the immediate parent. This appends the controller as
      // the first element of the target, which may not be the parent.
      this.parent.insertBefore(fragment, this.parent.firstChild);
      this.video.classList.add('vsc-initialized');
      this.video.dataset['vscid'] = this.id;
    }
  }

  function initializeWhenReady(document) {
    escapeStringRegExp.matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
    function escapeStringRegExp(str) {
      return str.replace(escapeStringRegExp.matchOperatorsRe, '\\$&');
    }

    var blacklisted = false;
    tc.settings.blacklist.split("\n").forEach(match => {
      match = match.replace(/^\s+|\s+$/g,'')
      if (match.length == 0) {
        return;
      }

      var regexp = new RegExp(escapeStringRegExp(match));
      if (regexp.test(location.href)) {
        blacklisted = true;
        return;
      }
    })

    if (blacklisted)
      return;

    var readyStateCheckInterval = setInterval(function() {
      if (document && document.readyState === 'complete') {
        clearInterval(readyStateCheckInterval);
        initializeNow(document);
      }
    }, 10);
  }

  function initializeNow(document) {
      if (document === window.document) {
        defineVideoController();
      } else {
        var link = document.createElement('link');
        link.href = chrome.extension.getURL('inject.css');
        link.type = 'text/css';
        link.rel = 'stylesheet';
        document.head.appendChild(link);
      }

      document.addEventListener('keydown', function(event) {
        var keyCode = event.keyCode;

        // Ignore if following modifier is active.
        if (event.getModifierState("Alt")
            || event.getModifierState("Control")
            || event.getModifierState("Fn")
            || event.getModifierState("Meta")
            || event.getModifierState("Hyper")
            || event.getModifierState("OS")) {
          return;
        }

        // Ignore keydown event if typing in an input box
        if ((document.activeElement.nodeName === 'INPUT'
              && document.activeElement.getAttribute('type') === 'text')
            || document.activeElement.nodeName === 'TEXTAREA'
            || document.activeElement.isContentEditable) {
          return false;
        }

        if (keyCode == tc.settings.rewindKeyCode) {
          runAction('rewind', document, true)
        } else if (keyCode == tc.settings.advanceKeyCode) {
          runAction('advance', document, true)
        } else if (keyCode == tc.settings.fasterKeyCode) {
          runAction('faster', document, true)
        } else if (keyCode == tc.settings.slowerKeyCode) {
          runAction('slower', document, true)
        } else if (keyCode == tc.settings.resetKeyCode) {
          runAction('reset', document, true)
        } else if (keyCode == tc.settings.displayKeyCode) {
          runAction('display', document, true)
        }

        return false;
      }, true);

      function checkForVideo(node, parent, added) {
        if (node.nodeName === 'VIDEO') {
          if (added) {
            if (!node.classList.contains('vsc-initialized')) {
              new tc.videoController(node, parent);
            }
          } else {
            if (node.classList.contains('vsc-initialized')) {
              let id = node.dataset['vscid'];
              let ctrl = document.querySelector(`div[data-vscid="${id}"]`)
              if (ctrl) {
                node.classList.remove('vsc-initialized');
                ctrl.remove();
              }
            }
          }
        } else if (node.children != undefined) {
          for (var i = 0; i < node.children.length; i++) {
            checkForVideo(node.children[i],
                          node.children[i].parentNode || parent,
                          added);
          }
        }
      }
      var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
          forEach.call(mutation.addedNodes, function(node) {
            if (typeof node === "function")
              return;
            checkForVideo(node, node.parentNode || mutation.target, true);
          })
          forEach.call(mutation.removedNodes, function(node) {
            if (typeof node === "function")
              return;
            checkForVideo(node, node.parentNode || mutation.target, false);
          })
        });
      });
      observer.observe(document, { childList: true, subtree: true });

      var videoTags = document.getElementsByTagName('video');
      forEach.call(videoTags, function(video) {
        new tc.videoController(video);
      });

      var frameTags = document.getElementsByTagName('iframe');
      forEach.call(frameTags, function(frame) {
        // Ignore frames we don't have permission to access (different origin).
        try { var childDocument = frame.contentDocument } catch (e) { return }
        initializeWhenReady(childDocument);
      });
  }

  function runAction(action, document, keyboard) {
    var videoTags = document.getElementsByTagName('video');
    videoTags.forEach = Array.prototype.forEach;

    videoTags.forEach(function(v) {
      var id = v.dataset['vscid'];
      var controller = document.querySelector(`div[data-vscid="${id}"]`);

      if (keyboard)
        showController(controller);

      if (!v.classList.contains('vsc-cancelled')) {
        if (action === 'rewind') {
          v.currentTime -= tc.settings.rewindTime;
        } else if (action === 'advance') {
          v.currentTime += tc.settings.advanceTime;
        } else if (action === 'faster') {
          // Maximum playback speed in Chrome is set to 16:
          // https://code.google.com/p/chromium/codesearch#chromium/src/media/blink/webmediaplayer_impl.cc&l=64
          var s = Math.min(v.playbackRate + tc.settings.speedStep, 16);
          v.playbackRate = Number(s.toFixed(2));
        } else if (action === 'slower') {
          // Audio playback is cut at 0.05:
          // https://code.google.com/p/chromium/codesearch#chromium/src/media/filters/audio_renderer_algorithm.cc&l=49
          var s = Math.max(v.playbackRate - tc.settings.speedStep, 0);
          v.playbackRate = Number(s.toFixed(2));
        } else if (action === 'reset') {
          v.playbackRate = 1.0;
        } else if (action === 'close') {
          v.classList.add('vsc-cancelled');
          controller.remove();
        } else if (action === 'display') {
          controller.classList.add('vsc-manual');
          controller.classList.toggle('vsc-hidden');
        } else if (action === 'drag') {
          handleDrag(v, controller);
        }
      }
    });
  }

 function handleDrag(video, controller) {
    const parentElement = controller.parentElement,
      boundRect = parentElement.getBoundingClientRect(),
      shadowController = controller.shadowRoot.querySelector('#controller'),
      drag = shadowController.querySelector('.draggable'),
      offsetLeft = boundRect.left + drag.offsetLeft + drag.offsetWidth,
      offsetTop = boundRect.top + drag.offsetTop + drag.offsetHeight;

    video.classList.add('vcs-dragging');
    shadowController.classList.add('dragging');

    const startDragging = (e) => {
      let newLeft = Math.max(0, e.clientX - offsetLeft);
      let newTop = Math.max(0, e.clientY - offsetTop);

      shadowController.style.left = newLeft + 'px';
      shadowController.style.top = newTop + 'px';
    }

    const stopDragging = () => {
      parentElement.removeEventListener('mousemove', startDragging);
      parentElement.removeEventListener('mouseup', stopDragging);
      parentElement.removeEventListener('mouseleave', stopDragging);

      shadowController.classList.remove('dragging');
      video.classList.remove('vcs-dragging');
    }

    parentElement.addEventListener('mouseup',stopDragging);
    parentElement.addEventListener('mouseleave',stopDragging);
    parentElement.addEventListener('mousemove', startDragging);
  }

  var timer;
  var animation = false;
  function showController(controller) {
    controller.classList.add('vcs-show');

    if (animation)
      clearTimeout(timer);

    animation = true;
    timer = setTimeout(function() {
      controller.classList.remove('vcs-show');
      animation = false;
    }, 2000);
  }
});


Video Speed Controller

short_name: videospeed

description: Speed up, slow down, advance and rewind any HTML5 video with quick shortcuts.

permissions: activeTab storage

repo_name: icanrealizeum/videospeed

license: unknown


code:
body {
	margin: 0;
	padding-left: 15px;
	padding-top: 53px;
	font-family: sans-serif;
	font-size: 12px;
	color: rgb(48, 57, 66);
}

h1, h2, h3 {
	font-weight: normal;
	line-height: 1;
    user-select: none;
	cursor: default;
}
h1 {
	font-size: 1.5em;
	margin: 21px 0 13px;
}
h3 {
	font-size: 1.2em;
	margin-bottom: 0.8em;
	color: black;
}
p {
	margin: 0.65em 0;
}

header {
	position: fixed;
	top: 0;
	left: 15px;
	right: 0;
	border-bottom: 1px solid #EEE;
	background: linear-gradient(white, white 40%, rgba(255, 255, 255, 0.92));
}
header, section {
	min-width: 600px;
	max-width: 738px;
}
section {
	padding-left: 18px;
	margin-top: 8px;
	margin-bottom: 24px;
}
section h3 {
	margin-left: -18px;
}

button {
	-webkit-appearance: none;
	position: relative;

	margin: 0 1px 0 0;
	padding: 0 10px;
	min-width: 4em;
	min-height: 2em;

	background-image: linear-gradient(#EDEDED, #EDEDED 38%, #DEDEDE);
	border: 1px solid rgba(0,0,0,0.25);
	border-radius: 2px;
	outline: none;
	box-shadow: 0 1px 0 rgba(0,0,0,0.08), inset 0 1px 2px rgba(255,255,255,0.75);
	color: #444;
	text-shadow: 0 1px 0 rgb(240,240,240);
	font: inherit;

    user-select: none;
}

input[type="text"] {
    width: 75px;
    text-align: center;
}

.row {
    margin: 5px 0px;
}

label {
    display: inline-block;
    width: 170px;
    vertical-align: top;
}

label[for=rememberSpeed] {
  width: 200px;
}

#status {
    color: #9D9D9D;
    display: inline-block;
    margin-left: 50px;
}

#faq {
	margin-top: 2em;
}


code:
<!DOCTYPE html>

		<title>Video Speed Controller: Options</title>
		<link rel="stylesheet" href="options.css" />

Video Speed Controller

Shortcuts

        Rewind
        <input id="rewindKeyInput" placeholder="press a key" type="text" value=""/>

        Advance
        <input id="advanceKeyInput" placeholder="press a key" type="text" value=""/>

        Reset speed
        <input id="resetKeyInput" placeholder="press a key" type="text" value=""/>

        Decrease speed
        <input id="slowerKeyInput" placeholder="press a key" type="text" value=""/>

        Increase speed
        <input id="fasterKeyInput" placeholder="press a key" type="text" value=""/>

        Show/hide controller
        <input id="displayKeyInput" placeholder="press a key" type="text" value=""/>

Other

          Rewind Time (s)
          <input id="rewindTime" type="text" value=""/>

          Advance Time (s)
          <input id="advanceTime" type="text" value=""/>

          Speed Change Step
          <input id="speedStep" type="text" value=""/>

          Remember Playback Speed
          <input id="rememberSpeed" type="checkbox"/>

          Blacklisted sites on which extension is disabled
(one per line)
          <textarea id="blacklist" rows="10" cols="50"></textarea>

    <button id="save">Save</button>
		<button id="restore">Restore Defaults</button>

The video controls are not showing up?

This extension is only compatible with HTML5 video. If you don't see the controls showing up, chances are you are viewing a Flash video. If you want to confirm, try right-clicking on the video and inspect the menu: if it mentions flash, then that's the issue. That said, most sites will fallback to HTML5 if they detect that Flash it not available. You can try manually disabling Flash plugin in Chrome:

In a new tab, navigate to chrome://plugins

Disable "Adobe Flash Player"

Restart your browser and try playing your video again

The speed controls are not showing up for local videos?

To enable playback of local media (e.g. File > Open File), you need to grant additional permissions to the extension.

In a new tab, navigate to chrome://extensions

Find "Video Speed Controller" extension in the list and enable "Allow access to file URLs"

Open a new tab and try opening a local file, the controls should show up


code:
var tcDefaults = {
  speed: 1.0,           // default 1x
  speedStep: 0.1,       // default 0.1x
  rewindTime: 10,       // default 10s
  advanceTime: 10,      // default 10s
  resetKeyCode:  82,    // default: R
  slowerKeyCode: 83,    // default: S
  fasterKeyCode: 68,    // default: D
  rewindKeyCode: 90,    // default: Z
  advanceKeyCode: 88,   // default: X
  displayKeyCode: 86,   // default: V
  rememberSpeed: true, // default: true
  blacklist: `
  `.replace(/^\s+|\s+$/gm,'')
  /*
    www.instagram.com
    twitter.com
    vine.co
    imgur.com
   */
};

var keyCodeAliases = {
  32:  'Space',
  96:  'Num 0',
  97:  'Num 1',
  98:  'Num 2',
  99:  'Num 3',
  100: 'Num 4',
  101: 'Num 5',
  102: 'Num 6',
  103: 'Num 7',
  104: 'Num 8',
  105: 'Num 9',
  106: 'Num *',
  107: 'Num +',
  109: 'Num -',
  110: 'Num .',
  111: 'Num /',
  186: ';',
  188: '<',
  189: '-',
  187: '+',
  190: '>',
  191: '/',
  192: '~',
  219: '[',
  220: '\\',
  221: ']',
  222: '\'',
}

function recordKeyPress(e) {
  if (
    (e.keyCode >= 48 && e.keyCode <= 57)    // Numbers 0-9
    || (e.keyCode >= 65 && e.keyCode <= 90) // Letters A-Z
    || keyCodeAliases[e.keyCode]            // Other character keys
  ) {
    e.target.value = keyCodeAliases[e.keyCode] || String.fromCharCode(e.keyCode);
    e.target.keyCode = e.keyCode;

    e.preventDefault();
    e.stopPropagation();
  } else if (e.keyCode === 8) { // Clear input when backspace pressed
    e.target.value = '';
  }
};

function inputFilterNumbersOnly(e) {
  var char = String.fromCharCode(e.keyCode);
  if (!/[\d\.]$/.test(char) || !/^\d+(\.\d*)?$/.test(e.target.value + char)) {
    e.preventDefault();
    e.stopPropagation();
  }
};

function inputFocus(e) {
   e.target.value = "";
};

function inputBlur(e) {
  e.target.value = keyCodeAliases[e.target.keyCode] || String.fromCharCode(e.target.keyCode);
};

function updateShortcutInputText(inputId, keyCode) {
  document.getElementById(inputId).value = keyCodeAliases[keyCode] || String.fromCharCode(keyCode);
  document.getElementById(inputId).keyCode = keyCode;
}

// Saves options to chrome.storage
function save_options() {

  var speedStep     = document.getElementById('speedStep').value;
  var rewindTime    = document.getElementById('rewindTime').value;
  var advanceTime   = document.getElementById('advanceTime').value;
  var resetKeyCode  = document.getElementById('resetKeyInput').keyCode;
  var rewindKeyCode = document.getElementById('rewindKeyInput').keyCode;
  var advanceKeyCode = document.getElementById('advanceKeyInput').keyCode;
  var slowerKeyCode = document.getElementById('slowerKeyInput').keyCode;
  var fasterKeyCode = document.getElementById('fasterKeyInput').keyCode;
  var displayKeyCode = document.getElementById('displayKeyInput').keyCode;
  var rememberSpeed = document.getElementById('rememberSpeed').checked;
  var blacklist     = document.getElementById('blacklist').value;

  speedStep     = isNaN(speedStep) ? tcDefaults.speedStep : Number(speedStep);
  rewindTime    = isNaN(rewindTime) ? tcDefaults.rewindTime : Number(rewindTime);
  advanceTime   = isNaN(advanceTime) ? tcDefaults.advanceTime : Number(advanceTime);
  resetKeyCode  = isNaN(resetKeyCode) ? tcDefaults.resetKeyCode : resetKeyCode;
  rewindKeyCode = isNaN(rewindKeyCode) ? tcDefaults.rewindKeyCode : rewindKeyCode;
  advanceKeyCode = isNaN(advanceKeyCode) ? tcDefaults.advanceKeyCode : advanceKeyCode;
  slowerKeyCode = isNaN(slowerKeyCode) ? tcDefaults.slowerKeyCode : slowerKeyCode;
  fasterKeyCode = isNaN(fasterKeyCode) ? tcDefaults.fasterKeyCode : fasterKeyCode;
  displayKeyCode = isNaN(displayKeyCode) ? tcDefaults.displayKeyCode : displayKeyCode;

  chrome.storage.sync.set({
    speedStep:      speedStep,
    rewindTime:     rewindTime,
    advanceTime:    advanceTime,
    resetKeyCode:   resetKeyCode,
    rewindKeyCode:  rewindKeyCode,
    advanceKeyCode: advanceKeyCode,
    slowerKeyCode:  slowerKeyCode,
    fasterKeyCode:  fasterKeyCode,
    displayKeyCode: displayKeyCode,
    rememberSpeed:  rememberSpeed,
    blacklist:      blacklist.replace(/^\s+|\s+$/gm,'')
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved';
    setTimeout(function() {
      status.textContent = '';
    }, 1000);
  });
}

// Restores options from chrome.storage
function restore_options() {
  chrome.storage.sync.get(tcDefaults, function(storage) {
    document.getElementById('speedStep').value = storage.speedStep.toFixed(2);
    document.getElementById('rewindTime').value = storage.rewindTime;
    document.getElementById('advanceTime').value = storage.advanceTime;
    updateShortcutInputText('resetKeyInput',  storage.resetKeyCode);
    updateShortcutInputText('rewindKeyInput', storage.rewindKeyCode);
    updateShortcutInputText('advanceKeyInput', storage.advanceKeyCode);
    updateShortcutInputText('slowerKeyInput', storage.slowerKeyCode);
    updateShortcutInputText('fasterKeyInput', storage.fasterKeyCode);
    updateShortcutInputText('displayKeyInput', storage.displayKeyCode);
    document.getElementById('rememberSpeed').checked = storage.rememberSpeed;
    document.getElementById('blacklist').value = storage.blacklist;
  });
}

function restore_defaults() {
  chrome.storage.sync.set(tcDefaults, function() {
    restore_options();
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Default options restored';
    setTimeout(function() {
      status.textContent = '';
    }, 1000);
  });
}

function initShortcutInput(inputId) {
  document.getElementById(inputId).addEventListener('focus', inputFocus);
  document.getElementById(inputId).addEventListener('blur', inputBlur);
  document.getElementById(inputId).addEventListener('keydown', recordKeyPress);
}

document.addEventListener('DOMContentLoaded', function () {
  restore_options();

  document.getElementById('save').addEventListener('click', save_options);
  document.getElementById('restore').addEventListener('click', restore_defaults);

  initShortcutInput('resetKeyInput');
  initShortcutInput('rewindKeyInput');
  initShortcutInput('advanceKeyInput');
  initShortcutInput('slowerKeyInput');
  initShortcutInput('fasterKeyInput');
  initShortcutInput('displayKeyInput');

  document.getElementById('rewindTime').addEventListener('keypress', inputFilterNumbersOnly);
  document.getElementById('advanceTime').addEventListener('keypress', inputFilterNumbersOnly);
  document.getElementById('speedStep').addEventListener('keypress', inputFilterNumbersOnly);
})


code:
body {
  min-width: 8em
}

hr {
  width: 100%;
  border: 0;
  height: 0;
  border-top: 1px solid rgba(0, 0, 0, 0.3);
  margin: 0.6em 0;
}

button {
  width: 100%;
  background-image: linear-gradient(#EDEDED, #EDEDED 38%, #DEDEDE);
  border: 1px solid rgba(0,0,0,0.25);
  border-radius: 2px;
  outline: none;
  box-shadow: 0 1px 0 rgba(0,0,0,0.08), inset 0 1px 2px rgba(255,255,255,0.75);
  color: #444;
  text-shadow: 0 1px 0 rgb(240,240,240);
  font: inherit;
  user-select: none;
}

.secondary {
  font-size: 0.95em;
  margin: 0.15em 0;
}


code:
<!doctype html>
<html>
  <head>
    <title>Video Speed Controller: Popup</title>
    <link rel="stylesheet" href="popup.css" />
    <script src="popup.js"></script>
  </head>
  <body>
    <button id="config">Settings</button>
    <hr />
    <button id="feedback" class="secondary">Send feedback</button>
    <button id="about" class="secondary">About</button>
  </body>
</html>


code:
document.addEventListener('DOMContentLoaded', function () {
  document.querySelector('#config').addEventListener('click', function() {
    window.open(chrome.extension.getURL("options.html"));
  });

  document.querySelector('#about').addEventListener('click', function() {
    window.open("https://github.com/igrigorik/videospeed");
  });

  document.querySelector('#feedback').addEventListener('click', function() {
    window.open("https://github.com/igrigorik/videospeed/issues");
  });
});


code:
* {
  line-height: 1.8em;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 13px;
}

:host(:hover) #controls {
  display: inline;
}

#controller {
  position: absolute;
  top: 0;
  left: 0;

  background: black;
  color: white;

  border-radius: 5px;
  padding: 5px;
  margin: 10px 10px 10px 15px;

  cursor: default;
  z-index: 9999999;
  opacity: 0.3;
}

#controller:hover {
  opacity: 0.7;
}

#controller:hover > .draggable {
  margin-right: 0.8em;
}

#controls {
  display: none;
}

#controller.dragging {
  cursor: -webkit-grabbing;
  opacity: 0.7;
}

#controller.dragging #controls {
  display: inline;
}

.draggable {
  cursor: -webkit-grab;
}

.draggable:active {
  cursor: -webkit-grabbing;
}

button {
  cursor: pointer;
  color: black;
  background: white;
  font-weight: bold;
  border-radius: 5px;
  padding: 1px 6px 3px 6px;
  font-size: 14px;
  line-height: 14px;
  border: 1px solid white;
  font-family: "Lucida Console", Monaco, monospace;
  margin-bottom: 2px;
}

button:focus {
  outline: 0;
}

button:hover {
  opacity: 1.0;
}

button:active {
  background: #ccc;
}

button.rw {
  opacity: 0.65;
}

button.hideButton {
  margin-right: 2px;
  opacity: 0.5;
}


code:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="PublishConfigData" autoUpload="Always" serverName="202">
    <serverData>
      <paths name="202">
        <serverdata>
          <mappings>
            <mapping deploy="." local="$PROJECT_DIR$" web="/" />
          </mappings>
        </serverdata>
      </paths>
      <paths name="QiniuP4">
        <serverdata>
          <mappings>
            <mapping deploy="." local="$PROJECT_DIR$" web="/" />
          </mappings>
        </serverdata>
      </paths>
    </serverData>
    <option name="myAutoUpload" value="ALWAYS" />
  </component>
</project>


code:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="WebServers">
    <option name="servers">
      <webServer id="22fcafc7-17e0-44e2-97b2-b53a8c7bc596" name="202" url="http://10.1.1.202">
        <fileTransfer host="10.1.1.202" port="22" rootFolder="/mnt/md0/Qiu/Code/TCN_pytorch" accessType="SFTP">
          <advancedOptions>
            <advancedOptions dataProtectionLevel="Private" />
          </advancedOptions>
          <option name="port" value="22" />
        </fileTransfer>
      </webServer>
    </option>
  </component>
</project>


code:
ACTNET200V13_JSON     = '/mnt/md0/Qiu/Code/TCN3/activity_net.v1-3.min.json'

# Use small Dataset
SMALL_DATASET = True
ACTNET200V13_PKL = None
if SMALL_DATASET == True:    ACTNET200V13_PKL = '/mnt/md1/Experiments/TCN_Test1/PKLS/small_actNet200-V1-3.pkl'
elif SMALL_DATASET == False: ACTNET200V13_PKL = '/mnt/md1/Experiments/TCN_Test1/PKLS/actNet200-V1-3.pkl'

TRAIN_PROPOSALS_PKL   = '/mnt/md1/Experiments/TCN_Test1/PKLS/train_proposals.pkl'
VAL_PROPOSALS_PKL     = '/mnt/md1/Experiments/TCN_Test1/PKLS/val_proposals.pkl'
TEST_PROPOSALS_PKL    = '/mnt/md1/Experiments/TCN_Test1/PKLS/test_proposals.pkl'

# TSN score 特征存放路径
TSNscore_FEATURE_PATH = '/mnt/md1/Dataset/ActivityNet/tsn_score/'
DPN107_FEATURE_PATH   = '/mnt/md1/Experiments/ExA_Test17/Features/'
TFLOG_ROOT_PATH = '/mnt/md1/Experiments/TFLOG/'

# Frame Count
FRAME_COUNT_PKL = '/mnt/md1/Experiments/GenProposals/proposals/frame_count.pkl'

#Feature PATH
DPN107_RGB_200_PATH = '/mnt/md1/Features/DPN107_RGB_201/'

if SMALL_DATASET==True:
    print('****Waring SMALL_DATASET IS OPEN!!!****\n'*10)


code:
import torch
from torch.utils.data import DataLoader,Dataset

class FeatureDataset(Dataset):

    def __init__(self,featurename,modality='train'):
        super(Dataset,self).__init__()
        self.modality = modality
        self.featurename = featurename
        if featurename not in ['tsn_score']:
            raise LookupError('{} not in given list'.format(featurename))
        if featurename == 'tsn_score':
            pass

    def __len__(self):
        pass

    def __index__(self):
        pass


code:
import pickle
from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL

class Dataset_DPN107(object):
    '''
    取数据, 分为test和train两种模式
    '''
    def __init__(self,subset,modality):
        '''
        :param modality:  test or train
        :param subset: testing validation traning
        '''
        self.modality = modality
        self.subset = subset

        self.load_groundtruth()
        self.load_proposals()

    def load_groundtruth(self):
        with open(ACTNET200V13_PKL,'rb') as f:
            groundtruth = pickle.load(f)['database']
        # filter by subset
        remainkeyset = []
        for key in groundtruth.keys():
            if groundtruth[key]['subset'] == self.subset:
                remainkeyset.append(key)
        self.groundtruth = dict()
        for key in remainkeyset:
            self.groundtruth[key] = groundtruth[key]
        self.vids = remainkeyset

    def load_proposals(self):
        PKL = None
        if self.subset == 'training': PKL = TRAIN_PROPOSALS_PKL
        elif self.subset == 'testing': PKL = TEST_PROPOSALS_PKL
        elif self.subset == 'validation': PKL = VAL_PROPOSALS_PKL
        with open(PKL,'rb') as f:
            self.proposals = pickle.load(f)

    def nextbatch(self):
        pass

    def enum_genfucntion(self):
        pass


code:
import pickle
import numpy as np
import torch
import os

from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL,DPN107_RGB_200_PATH,FRAME_COUNT_PKL
from DataSet.Tools_DPN107 import load_DPN107_from_file,extract_DPN107_feature
from DataSet.Utils import mulity_scale_pairs

class Dataset_DPN107_201_Class(object):
    '''
    取数据, 分为test和train两种模式
    '''
    def __init__(self,subset,modality):
        '''
        :param modality:  test or train
        :param subset: testing validation traning
        '''
        self.modality = modality
        self.subset = subset

        self.load_groundtruth()
        self.load_proposals()

        with open(FRAME_COUNT_PKL,'rb') as f:
            self.frame_count_dict = pickle.load(f)

    def load_groundtruth(self):
        with open(ACTNET200V13_PKL,'rb') as f:
            groundtruth = pickle.load(f)['database']
        # filter by subset
        remainkeyset = []
        for key in groundtruth.keys():
            if groundtruth[key]['subset'] == self.subset:
                remainkeyset.append(key)
        self.groundtruth = dict()
        for key in remainkeyset:
            self.groundtruth[key] = groundtruth[key]
        self.vids = remainkeyset

    def load_proposals(self):
        PKL = None
        if self.subset == 'training':
            PKL = TRAIN_PROPOSALS_PKL
        elif self.subset == 'testing':
            PKL = TEST_PROPOSALS_PKL
        elif self.subset == 'validation':
            PKL = VAL_PROPOSALS_PKL
        with open(PKL,'rb') as f:
            self.proposals = pickle.load(f)

    def _random_sample_traing_item(self,batch=2048, random=512, hard_neg=512, maxnump=32, PYRAMID=[12,12], thr_ious=[0.7,0.3]):
        '''
        无穷无尽的取训练用的batch
        :param batch:
        :param random:
        :param hard_neg:
        :param maxnump:
        :param PYRAMID:
        :param thr_ious:
        :return:
        '''
        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        x = np.zeros((batch, 1, np.sum(PYRAMID), 201))
        y = np.ones(batch)*-1

        for pos,vid in enumerate(self.vids):

            vitem = self.groundtruth[vid]
            if not os.path.exists(DPN107_RGB_200_PATH+'{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            if vid not in self.frame_count_dict:
                continue
            feat = load_DPN107_from_file(vid)
            # num_frame = vitem['numf']
            num_frame = self.frame_count_dict[vid]
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += mulity_scale_pairs(proposal[ind,], [lv])

            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_DPN107_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = pair[0,3]
                        pos_count += 1
                        count +=1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += mulity_scale_pairs(proposal[ind,], [i for i in range(6) if i!=lv])
            np.random.shuffle(rand_pairs)

            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_DPN107_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0] == np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = 0
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += mulity_scale_pairs(proposal[ind,])
            np.random.shuffle(neg_pairs)

            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_DPN107_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        return x,y

    def _enum_every_test_item(self):
        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if not os.path.exists(DPN107_RGB_200_PATH+'{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            if vid not in self.frame_count_dict:
                continue
            feat = load_DPN107_from_file(vid)
            # num_frame = vitem['numf']
            num_frame = self.frame_count_dict[vid]
            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # 64 * 6
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,2] = 0
                pairs = mulity_scale_pairs(current)
                ret = []
                index = []
                for pos,pair in enumerate(pairs):
                    x = extract_DPN107_feature(feat,pair,num_frame,duration,PYRAMID=[12,12])
                    if x is not None and x.shape[0] == 24:
                        # x1,x2 = x[:12,:],x[12:,:]
                        ret.append(x)
                        index.append(pos)
                # TODO 兼容有问题
                # 由于特征提取的原因, 有时候并不能正好得到6张图片
                ret = np.asarray(ret)
                current = current[index,...]
                yield [vid,current,ret]

    def nextbatch(self):
        return self._random_sample_traing_item()

    def enum_genfucntion(self):
        return self._enum_every_test_item()

if __name__=='__main__':

    dataset = Dataset_DPN107_201_Class('validation', 'test')

    X,Y = dataset.nextbatch()

    import IPython;IPython.embed()


code:
import pickle
import numpy as np
import torch
import os

from DataSet.Tools_DPN107 import load_DPN107_from_file,extract_DPN107_feature
from DataSet.Dataset_DPN107 import Dataset_DPN107

class Dataset_DPN107_Class(Dataset_DPN107):

    def tempo_bilinear(self,feat):
        return np.dot(np.transpose(feat),feat)

    def _random_sample_traing_item(self,batch=1024, random=32, hard_neg=32, maxnump=32, thr_ious=[0.7,0.3]):

        x = np.zeros((batch,12,2688))
        y = np.ones((batch))*-1

        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        for pos, vid in enumerate(self.vids):
            vitem = self.groundtruth[vid]
            if id not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_DPN107_from_file(vid)
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            PYRAMID = [12]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += [proposal[ind,lv:lv+1,:]]
            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...] = data
                        y[count, ...] = pair[0,3]
                        pos_count += 1
                        count += 1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += [proposal[ind,i:i+1,:] for i in range(6) if i!=lv]
            np.random.shuffle(rand_pairs)
            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...] = data
                        y[count, ...] = pair[0,3]
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += [ proposal[ind,i:i+1,] for i in range(6) ]
            np.random.shuffle(neg_pairs)
            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...] = data
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        return x,y

    def nextbatch(self):
        return self._random_sample_traing_item()

    def _enum_every_test_item(self):

        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if id not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_DPN107_from_file(vid)

            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # proposals: 64*6 64个锚点每个锚点6个不同长度
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,3] = 0
                n = current.shape[0]
                inputx = extract_DPN107_feature(feat,current,duration,PYRAMID=[10000]*n,SpliteData=True,repeat=False)
                if inputx is None: continue
                INPT = np.zeros((n,202*202))
                index = []
                for j in range(n):
                    if inputx[j] is None: continue
                    index.append(j)
                    INPT[j,...] = self.tempo_bilinear(inputx[j]).flatten()
                current = current[index,:]
                INPT = INPT[index,:]
                yield [vid,current,INPT]

    def enum_genfucntion(self):
        return self._enum_every_test_item()

if __name__=='__main__':

    dataset = Dataset_DPN107_Class('validation','train')
    x,y = dataset.nextbatch()


code:
import pickle
import numpy as np
import torch
import os

from DataSet.Dataset_DPN107 import Dataset_DPN107
from DataSet.Tools_DPN107 import load_DPN107_from_file,extract_DPN107_feature
from DataSet.Utils import mulity_scale_pairs

class Dataset_DPN107_Pair(Dataset_DPN107):

    def _random_sample_traing_item(self,batch=2048, random=512, hard_neg=512, maxnump=32, PYRAMID=[12,12], thr_ious=[0.7,0.3]):
        '''
        无穷无尽的取训练用的batch
        :param batch:
        :param random:
        :param hard_neg:
        :param maxnump:
        :param PYRAMID:
        :param thr_ious:
        :return:
        '''
        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        x = np.zeros((batch, 1, np.sum(PYRAMID), 2688))
        y = np.ones(batch)*-1

        for pos,vid in enumerate(self.vids):

            vitem = self.groundtruth[vid]

            if os.path.exists('/mnt/md1/Experiments/ExA_Test17/Features/dpn107_{}.pkl'.format(vid)) == False \
                    or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue

            feat = load_DPN107_from_file(vid)
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += mulity_scale_pairs(proposal[ind,], [lv])

            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 1
                        pos_count += 1
                        count += 1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += mulity_scale_pairs(proposal[ind,], [i for i in range(6) if i!=lv])
            np.random.shuffle(rand_pairs)

            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0] == np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 0
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += mulity_scale_pairs(proposal[ind,])
            np.random.shuffle(neg_pairs)

            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_DPN107_feature(feat, pair, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        return x,y

    def _enum_every_test_item(self):
        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if os.path.exists('/mnt/md1/Experiments/ExA_Test17/Features/dpn107_{}.pkl'.format(vid)) == False or \
                    vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue

            feat = load_DPN107_from_file(vid)
            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # 64 * 6
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,2] = 0
                pairs = mulity_scale_pairs(current)
                ret = []
                index = []
                for pos,pair in enumerate(pairs):
                    x = extract_DPN107_feature(feat,pair,duration,PYRAMID=[12,12],repeat=True)
                    if x is not None and x.shape[0] == 24:
                        ret.append(x)
                        index.append(pos)
                # TODO 兼容有问题
                # 由于特征提取的原因, 有时候并不能正好得到6张图片
                ret = np.asarray(ret)
                current = current[index,...]
                yield [vid,current,ret]

    def nextbatch(self):
        return self._random_sample_traing_item()

    def enum_genfucntion(self):
        return self._enum_every_test_item()

if __name__=='__main__':
    model = Dataset_DPN107_Pair('validation','train')
    x,y = model.nextbatch()


code:
import pickle
import numpy as np
import torch
import os

from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL,TSNscore_FEATURE_PATH
from DataSet.Tools_TSNscore import load_TSNscore_from_file,extract_TSN_feature
from DataSet.Utils import mulity_scale_pairs

class Dataset_TSNscore(object):
    '''
    取数据, 分为test和train两种模式
    '''
    def __init__(self,subset,modality):
        '''
        :param modality:  test or train
        :param subset: testing validation traning
        '''
        self.modality = modality
        self.subset = subset

        self.load_groundtruth()
        self.load_proposals()

    def load_groundtruth(self):
        with open(ACTNET200V13_PKL,'rb') as f:
            groundtruth = pickle.load(f)['database']
        # filter by subset
        remainkeyset = []
        for key in groundtruth.keys():
            if groundtruth[key]['subset'] == self.subset:
                remainkeyset.append(key)
        self.groundtruth = dict()
        for key in remainkeyset:
            self.groundtruth[key] = groundtruth[key]
        self.vids = remainkeyset

    def load_proposals(self):
        PKL = None
        if self.subset == 'training':
            PKL = TRAIN_PROPOSALS_PKL
        elif self.subset == 'testing':
            PKL = TEST_PROPOSALS_PKL
        elif self.subset == 'validation':
            PKL = VAL_PROPOSALS_PKL
        with open(PKL,'rb') as f:
            self.proposals = pickle.load(f)

    def nextbatch(self):
        pass

    def enum_genfucntion(self):
        pass


code:
import pickle
import numpy as np
import torch
import os

from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL,TSNscore_FEATURE_PATH
from DataSet.Tools_TSNscore import load_TSNscore_from_file,extract_TSN_feature
from DataSet.Dataset_TSNscore import Dataset_TSNscore

class Dataset_TSNscore_Class(Dataset_TSNscore):

    def tempo_bilinear(self,feat):
        return np.dot(np.transpose(feat),feat)

    def _random_sample_traing_item(self,batch=1024, random=32, hard_neg=32, maxnump=32, thr_ious=[0.7,0.3]):
        x = np.zeros((batch,202*202))
        y = np.ones((batch))*-1

        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        for pos, vid in enumerate(self.vids):
            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue

            feat = load_TSNscore_from_file(vid)
            num_frame = vitem['numf']
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            PYRAMID = [10000]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += [proposal[ind,lv:lv+1,:]]
            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...].flat = self.tempo_bilinear(data).flatten()
                        y[count, ...] = pair[0,3]
                        pos_count += 1
                        count += 1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += [proposal[ind,i:i+1,:] for i in range(6) if i!=lv]
            np.random.shuffle(rand_pairs)
            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...].flat = self.tempo_bilinear(data).flatten()
                        y[count, ...] = pair[0,3]
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += [ proposal[ind,i:i+1,] for i in range(6) ]
            np.random.shuffle(neg_pairs)
            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID,repeat=False)
                    if data is not None :
                        x[count, ...].flat = self.tempo_bilinear(data).flatten()
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        return x,y

    def nextbatch(self):
        return self._random_sample_traing_item()

    def _enum_every_test_item(self):

        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_TSNscore_from_file(vid)

            num_frame = vitem['numf']
            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # proposals: 64*6 64个锚点每个锚点6个不同长度
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,3] = 0
                n = current.shape[0]
                inputx = extract_TSN_feature(feat,current,num_frame,duration,PYRAMID=[10000]*n,SpliteData=True,repeat=False)
                if inputx is None: continue
                INPT = np.zeros((n,202*202))
                index = []
                for j in range(n):
                    if inputx[j] is None: continue
                    index.append(j)
                    INPT[j,...] = self.tempo_bilinear(inputx[j]).flatten()
                current = current[index,:]
                INPT = INPT[index,:]
                yield [vid,current,INPT]

    def enum_genfucntion(self):
        return self._enum_every_test_item()

def test1():
    dataset = Dataset_TSNscore_Class('validation', 'train')
    x,y = dataset.nextbatch()
    # x: (1024,40804) y: (1024,)

def test2():
    dataset = Dataset_TSNscore_Class('validation', 'test')
    gen = dataset.enum_genfucntion()
    vid,cur,inputx = next(gen)

if __name__=='__main__':
    test2()


code:
import pickle
import numpy as np
import torch
import os

from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL,TSNscore_FEATURE_PATH
from DataSet.Tools_TSNscore import load_TSNscore_from_file,extract_TSN_feature
from DataSet.Utils import mulity_scale_pairs

class Dataset_TSNscore_Class_nobi(object):
    '''
    取数据, 分为test和train两种模式
    '''
    def __init__(self,subset,modality):
        '''
        :param modality:  test or train
        :param subset: testing validation traning
        '''
        self.modality = modality
        self.subset = subset

        self.load_groundtruth()
        self.load_proposals()

    def load_groundtruth(self):
        with open(ACTNET200V13_PKL,'rb') as f:
            groundtruth = pickle.load(f)['database']
        # filter by subset
        remainkeyset = []
        for key in groundtruth.keys():
            if groundtruth[key]['subset'] == self.subset:
                remainkeyset.append(key)
        self.groundtruth = dict()
        for key in remainkeyset:
            self.groundtruth[key] = groundtruth[key]
        self.vids = remainkeyset

    def load_proposals(self):
        PKL = None
        if self.subset == 'training':
            PKL = TRAIN_PROPOSALS_PKL
        elif self.subset == 'testing':
            PKL = TEST_PROPOSALS_PKL
        elif self.subset == 'validation':
            PKL = VAL_PROPOSALS_PKL
        with open(PKL,'rb') as f:
            self.proposals = pickle.load(f)

    def _random_sample_traing_item(self,batch=2048, random=512, hard_neg=512, maxnump=32, PYRAMID=[12,12], thr_ious=[0.7,0.3]):
        '''
        无穷无尽的取训练用的batch
        :param batch:
        :param random:
        :param hard_neg:
        :param maxnump:
        :param PYRAMID:
        :param thr_ious:
        :return:
        '''
        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        x = np.zeros((batch, 1, np.sum(PYRAMID), 202))
        y = np.ones(batch)*-1

        for pos,vid in enumerate(self.vids):

            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_TSNscore_from_file(vid)
            num_frame = vitem['numf']
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += mulity_scale_pairs(proposal[ind,], [lv])

            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = pair[0,3]
                        pos_count += 1
                        count +=1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += mulity_scale_pairs(proposal[ind,], [i for i in range(6) if i!=lv])
            np.random.shuffle(rand_pairs)

            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0] == np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = 0
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += mulity_scale_pairs(proposal[ind,])
            np.random.shuffle(neg_pairs)

            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...] = data
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        return x,y

    def _enum_every_test_item(self):
        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_TSNscore_from_file(vid)
            num_frame = vitem['numf']
            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # 64 * 6
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,2] = 0
                pairs = mulity_scale_pairs(current)
                ret = []
                index = []
                for pos,pair in enumerate(pairs):
                    x = extract_TSN_feature(feat,pair,num_frame,duration,PYRAMID=[12,12])
                    if x is not None and x.shape[0] == 24:
                        # x1,x2 = x[:12,:],x[12:,:]
                        ret.append(x)
                        index.append(pos)
                # TODO 兼容有问题
                # 由于特征提取的原因, 有时候并不能正好得到6张图片
                ret = np.asarray(ret)
                current = current[index,...]
                yield [vid,current,ret]

    def nextbatch(self):
        return self._random_sample_traing_item()

    def enum_genfucntion(self):
        return self._enum_every_test_item()

if __name__=='__main__':

    dataset = Dataset_TSNscore_Class_nobi('validation', 'test')

    X,Y = dataset.nextbatch()

    import IPython;IPython.embed()


code:
import pickle
import numpy as np
import torch
import os

from Config.Config import ACTNET200V13_PKL,TRAIN_PROPOSALS_PKL,TEST_PROPOSALS_PKL,VAL_PROPOSALS_PKL,TSNscore_FEATURE_PATH
from DataSet.Tools_TSNscore import load_TSNscore_from_file,extract_TSN_feature
from DataSet.Utils import mulity_scale_pairs

class Dataset_TSNscore_Pair(object):
    '''
    取数据, 分为test和train两种模式
    '''
    def __init__(self,subset,modality,addnoize=False):
        '''
        :param modality:  test or train
        :param subset: testing validation traning
        '''
        self.modality = modality
        self.subset = subset
        self.addnoize = False

        self.load_groundtruth()
        self.load_proposals()

    def load_groundtruth(self):
        with open(ACTNET200V13_PKL,'rb') as f:
            groundtruth = pickle.load(f)['database']
        # filter by subset
        remainkeyset = []
        for key in groundtruth.keys():
            if groundtruth[key]['subset'] == self.subset:
                remainkeyset.append(key)
        self.groundtruth = dict()
        for key in remainkeyset:
            self.groundtruth[key] = groundtruth[key]
        self.vids = remainkeyset

    def load_proposals(self):
        PKL = None
        if self.subset == 'training':
            PKL = TRAIN_PROPOSALS_PKL
        elif self.subset == 'testing':
            PKL = TEST_PROPOSALS_PKL
        elif self.subset == 'validation':
            PKL = VAL_PROPOSALS_PKL
        with open(PKL,'rb') as f:
            self.proposals = pickle.load(f)

    def _random_sample_traing_item(self,batch=2048, random=512, hard_neg=512, maxnump=32, PYRAMID=[12,12], thr_ious=[0.7,0.3]):
        '''
        无穷无尽的取训练用的batch
        :param batch:
        :param random:
        :param hard_neg:
        :param maxnump:
        :param PYRAMID:
        :param thr_ious:
        :return:
        '''
        target_pos = batch-hard_neg-random
        pos_count = 0
        neg_count = 0
        rand_count = 0
        count = 0

        x = np.zeros((batch, 1, np.sum(PYRAMID), 202))
        y = np.ones(batch)*-1

        for pos,vid in enumerate(self.vids):

            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_TSNscore_from_file(vid)
            num_frame = vitem['numf']
            duration = vitem['duration']
            proposal = np.asarray(self.proposals[vid])

            max_ious = proposal[:,:,2].max(axis=1)
            max_ious_lv = proposal[:,:,2].argmax(axis=1)

            pos_indexes = np.nonzero(max_ious>thr_ious[0])[0]
            neg_indexes = np.nonzero(max_ious<thr_ious[1])[0]

            pos_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                pos_pairs += mulity_scale_pairs(proposal[ind,], [lv])

            np.random.shuffle(pos_pairs)
            for pair in pos_pairs[:min(target_pos,maxnump)]:
                if pos_count < target_pos and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 1
                        pos_count += 1
                        count +=1

            # sample random negative around around positive
            rand_pairs = []
            for i in range(len(pos_indexes)):
                ind = pos_indexes[i]
                lv = max_ious_lv[ind]
                rand_pairs += mulity_scale_pairs(proposal[ind,], [i for i in range(6) if i!=lv])
            np.random.shuffle(rand_pairs)

            for pair in rand_pairs[:min(random,maxnump)]:
                if rand_count<random and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0] == np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 0
                        rand_count += 1
                        count += 1

            # sample hard negatives
            neg_pairs = []
            for i in range(len(neg_indexes)):
                ind = neg_indexes[i]
                neg_pairs += mulity_scale_pairs(proposal[ind,])
            np.random.shuffle(neg_pairs)

            for pair in neg_pairs[:min(hard_neg,maxnump)]:
                if neg_count < hard_neg and count < batch:
                    data = extract_TSN_feature(feat, pair, num_frame, duration, PYRAMID=PYRAMID)
                    if data is not None and data.shape[0]==np.sum(PYRAMID):
                        x[count, ...].flat = data.flat
                        y[count, ...] = 0
                        neg_count += 1
                        count += 1

            if count == batch:
                # 位移动一下
                self.vids = self.vids[pos:] + self.vids[:pos]
                break

        if self.addnoize == True:
            SP = x.shape
            x  += np.random.normal(0.0001,1,SP)

        return x,y

    def _enum_every_test_item(self):
        # 一次出来一组6个和对应的VID
        for vid,proposals in self.proposals.items():

            vitem = self.groundtruth[vid]
            if not os.path.exists(TSNscore_FEATURE_PATH+'/feat/{}.h5'.format(vid)) or vid not in self.proposals or len(self.proposals[vid]) == 0:
                continue
            feat = load_TSNscore_from_file(vid)
            num_frame = vitem['numf']
            duration = vitem['duration']
            proposals = np.asarray(proposals)

            # 64 * 6
            for i in range(proposals.shape[0]):
                current = np.copy(proposals[i,...])
                # current shape 6x4
                current[:,2] = 0
                pairs = mulity_scale_pairs(current)
                ret = []
                index = []
                for pos,pair in enumerate(pairs):
                    x = extract_TSN_feature(feat,pair,num_frame,duration,PYRAMID=[12,12])
                    if x is not None and x.shape[0] == 24:
                        # x1,x2 = x[:12,:],x[12:,:]
                        ret.append(x)
                        index.append(pos)
                # TODO 兼容有问题
                # 由于特征提取的原因, 有时候并不能正好得到6张图片
                ret = np.asarray(ret)
                current = current[index,...]
                yield [vid,current,ret]

    def nextbatch(self):
        return self._random_sample_traing_item()

    def enum_genfucntion(self):
        return self._enum_every_test_item()

def test():
    dataset = Dataset_TSNscore_Pair('validation', 'train')
    x,y = dataset.nextbatch()

if __name__=='__main__':
    dataset = Dataset_TSNscore_Pair('validation', 'test')
    gen = dataset.enum_genfucntion()
    # b = next(gen)
    # b[2].shape
    # cnt = 0
    # for b in next(gen):
    #     cnt += 1
    #     if cnt > 100:
    #         break


code:
import os
import pickle
import numpy as np
import h5py
import random
import math

from Config.Config import DPN107_FEATURE_PATH
from Config.Config import DPN107_RGB_200_PATH

from functools import lru_cache

@lru_cache(maxsize=1000)
def load_DPN107_from_file(vid):
    try:
        with h5py.File(os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid)),'r') as f:
            d = f['feature201'][:]
            return d
    except Exception as E:
        raise E

def extract_DPN107_from_proposal(feat, num_frame, duration, start, end, K, zero_padding=False, repeat=True):

    # K 大于 len 时如果 repeat=False 则取出全部的帧

    fps = num_frame / duration
    numfeat = feat.shape[0]
    indexes = np.asarray(np.arange(numfeat))

    # 特征长度和视频的长度对不上!!!
    startframe = round(start*fps)/round(fps*0.4)
    endframe = round(end*fps)/round(fps*0.4)

    #get in range indexes
    inrange = np.nonzero([np.logical_and(indexes<=endframe, indexes>=startframe)])[1]

    if len(inrange)==0: return None

    # 不管怎么样采了selected帧
    # TODO what's this?
    start_out = max(0-start,0)
    end_out = max(end-duration,0)

    if (start_out+end_out)>0 and zero_padding:
        start_zeros = int(round(start_out*K/(end-start)))
        end_zeros = int(round(end_out*K/(end-start)))

        nselect = K-start_zeros-end_zeros
        if len(inrange)<nselect and not repeat:
            selected = inrange
        else:
            inds = np.floor(np.arange(0,len(inrange)-1e-6,float(len(inrange))/nselect)+float(len(inrange))/(2*nselect)).astype(int)
            selected = inrange[inds]

        data = np.vstack((np.zeros((start_zeros, feat.shape[1])), feat[selected,:], np.zeros((end_zeros, feat.shape[1]))))
    else:
        if len(inrange)<K and not repeat:
            selected = inrange
        else:
            inds = np.floor(np.arange(0,len(inrange)-1e-6,float(len(inrange))/K)+float(len(inrange))/(2*K)).astype(int)
            selected = inrange[inds]

        data = feat[selected,:]
    #
    # print('EXTRACT_tsnSCORE_FROM_PROPOSAL:')
    # print('feat_shape:',feat.shape)
    # print('num_frame:',num_frame)
    # print('duration:',duration)
    # print('start:',start,' end:',end)
    # print('K:',K)
    # print('zero_pad',zero_padding,' repeat',repeat)
    # print('fps:',fps)
    # print('starteframe:',startframe,'endframe:',endframe)
    # print('start_out:',start_out,'end_out:',end_out)
    # condition = ((start_out+end_out)>0 and zero_padding)
    # print('IF Condition',condition)
    # if condition:
    #     print('start_zeros:',start_zeros,' end_zeros:',end_zeros)
    #     print('nselect:',nselect)
    # import IPython;IPython.embed()

    return data

def extract_DPN107_feature(feature, proposal, num_frame, duration, PYRAMID=[12,12],SpliteData=False,**kwargs):
    # 抽取特征
    data = []
    for ind, num in enumerate(PYRAMID):
        feat = extract_DPN107_from_proposal(feature, num_frame, duration, proposal[ind,0], proposal[ind,1], num, zero_padding=True, **kwargs)
        if feat is None: return None
        data.append(feat)
    if SpliteData == False: return np.vstack(data)
    elif SpliteData == True: return data

def test1():
    vid = '_kG3DxvGnnQ'
    feat = load_DPN107_from_file(vid)

if __name__=='__main__':
    vid = 'Zxi0V2pBPlA'
    feat = load_DPN107_from_file(vid)


code:
import os
import h5py
import numpy as np

from Config.Config import TSNscore_FEATURE_PATH as FEATURE
from DataSet.Utils import mulity_scale_pairs
from functools import lru_cache

@lru_cache(maxsize=5000)
def load_TSNscore_from_file(vid):
    with h5py.File(FEATURE + '/feat/%s.h5' % vid, 'r') as hf:
        fg = np.asarray(hf['fg'])
        bg = np.asarray(hf['bg'])
        feat1 = np.hstack([fg,bg])
    with h5py.File(FEATURE + '/flow/%s.h5' % vid, 'r') as hf:
        fg2 = np.asarray(hf['fg']) # 200
        bg2 = np.asarray(hf['bg']) # 2
        feat2 = np.hstack([fg2,bg2])
    feat = feat1 + feat2
    return feat

# def load_TSNscore_from_file(vid):
#     from Config.Config import DPN107_RGB_200_PATH
#     try:
#         with h5py.File(os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid)),'r') as f:
#             d = f['feature201'][:]
#             return d
#     except Exception as E:
#         raise E

def extract_TSNscore_from_proposal(feat, num_frame, duration, start, end, K, zero_padding=False, repeat=True):
    '''
    :param feat:  特征
    :param num_frame: 视频长度
    :param duration: 视频持续时间
    :param start: 开始的时间
    :param end: 结束的时间
    :param K: 采样数
    :param zero_padding:
    :param repeat:
    :return: 采样的特征 或者 None
    '''

    # K 大于 len 时如果 repeat=False 则取出全部的帧

    fps = num_frame / duration
    numfeat = feat.shape[0]
    indexes = np.asarray(np.arange(numfeat))

    # 特征长度和视频的长度对不上!!!
    startframe = round(start*fps)/round(fps*0.4)
    endframe = round(end*fps)/round(fps*0.4)
    #get in range indexes
    inrange = np.nonzero([np.logical_and(indexes<=endframe, indexes>=startframe)])[1]

    if len(inrange)==0: return None

    # 不管怎么样采了selected帧
    # TODO what's this?
    start_out = max(0-start,0)
    end_out = max(end-duration,0)

    # 开始点和结束点超过了应有的范围并且需要补0
    if (start_out+end_out)>0 and zero_padding:
        start_zeros = int(round(start_out*K/(end-start)))
        end_zeros = int(round(end_out*K/(end-start)))

        nselect = K-start_zeros-end_zeros
        if len(inrange)<nselect and not repeat:
            selected = inrange
        else:
            inds = np.floor(np.arange(0,len(inrange)-1e-6,float(len(inrange))/nselect)+float(len(inrange))/(2*nselect)).astype(int)
            selected = inrange[inds]

        data = np.vstack((np.zeros((start_zeros, feat.shape[1])), feat[selected,:], np.zeros((end_zeros, feat.shape[1]))))
    else:
        if len(inrange)<K and not repeat:
            selected = inrange
        else:
            inds = np.floor(np.arange(0,len(inrange)-1e-6,float(len(inrange))/K)+float(len(inrange))/(2*K)).astype(int)
            selected = inrange[inds]

        data = feat[selected,:]
    #
    # print('EXTRACT_tsnSCORE_FROM_PROPOSAL:')
    # print('feat_shape:',feat.shape)
    # print('num_frame:',num_frame)
    # print('duration:',duration)
    # print('start:',start,' end:',end)
    # print('K:',K)
    # print('zero_pad',zero_padding,' repeat',repeat)
    # print('fps:',fps)
    # print('starteframe:',startframe,'endframe:',endframe)
    # print('start_out:',start_out,'end_out:',end_out)
    # condition = ((start_out+end_out)>0 and zero_padding)
    # print('IF Condition',condition)
    # if condition:
    #     print('start_zeros:',start_zeros,' end_zeros:',end_zeros)
    #     print('nselect:',nselect)
    # import IPython;IPython.embed()

    return data

def extract_TSN_feature(feature, proposal, num_frame, duration, PYRAMID=[12,12],SpliteData=False,**kwargs):
    # 抽取特征
    data = []
    for ind, num in enumerate(PYRAMID):
        feat = extract_TSNscore_from_proposal(feature, num_frame, duration, proposal[ind,0], proposal[ind,1], num, zero_padding=True, **kwargs)
        if feat is None: return None
        data.append(feat)
    if SpliteData == False:
        return np.vstack(data)
    elif SpliteData == True:
        return data

def test_loadfature():
    vid = 'sJFgo9H6zNo'
    feat = load_TSNscore_from_file(vid)
    print(feat.shape)


code:
import numpy as np

# Sample propsoal pairs with a pyramid around specific level (a list) if given
def mulity_scale_pairs(proposals, ids=None, scale=2):
    '''
    :return: [ [ len(ids) x 4 ] ... ]
    '''
    pairs = []
    num = proposals.shape[0]

    if ids is not None: plist = ids
    else: plist = list(range(num))

    for i in plist:
        # 原有特征放在 维度0
        # 左右特征扩大一点放在 维度1
        top = np.zeros((2, 4))
        top[0, :] = proposals[i, :]

        center = (proposals[i, 0] + proposals[i, 1]) / 2
        length = (proposals[i, 1] - proposals[i, 0]) / 2
        top[1, 0] = center - length*scale
        top[1, 1] = center + length*scale
        pairs.append(top)

    return pairs


code:
import pickle
from DataSet.Tools_TSNscore import load_TSNscore

filepath = '/mnt/md0/Qiu/Code/TCN3/actNet200-V1-3.pkl'

with open(filepath,'rb') as f:
    d = pickle.load(f)

d.keys()
# dict_keys(['taxonomy', 'version', 'actionIDs', 'database'])

database = d['database']
keys = list(database.keys())
database[keys[10]]

'''
增加了:
       class
       sf,ef
       fps
       numf

Out[8]:
{'annotations': [{'class': 106,
   'ef': 2488,
   'label': 'Shoveling snow',
   'nodeid': 183,
   'segment': [0.01, 83.05780045351474],
   'sf': 0}],
 'duration': 83.06,
 'fps': 29.97002997002997,
 'isnull': 0,
 'newResolution': [256, 342],
 'numf': 2488,
 'resolution': '640x480',
 'subset': 'training',
 'url': 'https://www.youtube.com/watch?v=hHiPEAiYKv0'}

'''

# 检查特征长度和frame长度是否是对应的

eq = 0
neq = 0
cnt = 0

for key in keys:
    D = database[key]
    if D['subset'] == 'testing':
        continue
    numf = D['numf']
    try:
        feat = load_TSNscore(key)
    except Exception as E:
        continue

    cnt += 1
    featnum = feat.shape[0]

    ffn = featnum/0.4

    w = input()
    print(featnum,numf,ffn)

    if featnum != numf :
        neq+=1
    else :
        eq+=1

    if cnt%20 == 0:
        print('eq ',eq,' neq ',neq)


code:
import caffe
import tempfile
import numpy as np

def create_net():
    f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    f.write(
        """
        name: 'pair_nn'

        #level base
        layer { name: "data" type: "DummyData" top: "lv" dummy_data_param { shape { dim: 2048 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv' type: 'Convolution' bottom: "lv" top: "conv" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu" type: "ReLU" bottom: "conv" top: "conv" }
        layer { name: "pool" type: "Pooling" bottom: "conv" top: "pool" pooling_param { pool: AVE kernel_h: 3 kernel_w: 1 stride_h: 1 stride_w: 1} }
        #level upper
        layer { name: "data_upper" type: "DummyData" top: "lv_upper" dummy_data_param { shape { dim: 2048 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv_upper' type: 'Convolution' bottom: "lv_upper" top: "conv_upper" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu_upper" type: "ReLU" bottom: "conv_upper" top: "conv_upper" }
        layer { name: "pool_upper" type: "Pooling" bottom: "conv_upper" top: "pool_upper" pooling_param { pool: AVE kernel_h: 3  kernel_w: 1 stride_h: 1 stride_w: 1} }

        layer { name: "concat" type: "Concat" bottom: "pool" bottom: "pool_upper" top: "ip1" concat_param { axis: 1 } }

        #proposal loss
        layer { name: "label" type: "DummyData" top: "label" dummy_data_param { shape { dim: 2048 dim: 1 dim: 1 dim: 1 } } }
        layer { name: "ip1" type: "InnerProduct" bottom: "ip1" top: "ip2" inner_product_param { num_output: 500 weight_filler {type: "xavier"} } }
        layer { name: "ip2" type: "InnerProduct" bottom: "ip2" top: "rs" inner_product_param { num_output: 2 weight_filler {type: "xavier"} } }
        layer { name: "loss" type: "SoftmaxWithLoss" bottom: "rs" bottom: "label" top: "loss" loss_weight: 1 loss_param {ignore_label: -1 normalize: true} }
        """)
    f.close()
    return f.name

def create_deploy():
    f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    f.write(
        """
        name: 'pair_nn'

        #level base
        layer { name: "data" type: "DummyData" top: "lv" dummy_data_param { shape { dim: 1 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv' type: 'Convolution' bottom: "lv" top: "conv" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu" type: "ReLU" bottom: "conv" top: "conv" }
        layer { name: "pool" type: "Pooling" bottom: "conv" top: "pool" pooling_param { pool: AVE kernel_h: 3 kernel_w: 1 stride_h: 1 stride_w: 1} }
        # #level upper
        # layer { name: "data_upper" type: "DummyData" top: "lv_upper" dummy_data_param { shape { dim: 1 dim: 1 dim: 12 dim: 202 } } }
        # layer { name: 'conv_upper' type: 'Convolution' bottom: "lv_upper" top: "conv_upper" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        # layer { name: "relu_upper" type: "ReLU" bottom: "conv_upper" top: "conv_upper" }
        # layer { name: "pool_upper" type: "Pooling" bottom: "conv_upper" top: "pool_upper" pooling_param { pool: AVE kernel_h: 3  kernel_w: 1 stride_h: 1 stride_w: 1} }
        #
        # layer { name: "concat" type: "Concat" bottom: "pool" bottom: "pool_upper" top: "ip1" concat_param { axis: 1 } }
        #
        #proposal
        # layer { name: "ip1" type: "InnerProduct" bottom: "ip1" top: "ip2" inner_product_param { num_output: 500 } }
        # layer { name: "ip2" type: "InnerProduct" bottom: "ip2" top: "rs" inner_product_param { num_output: 2 } }
        # layer { name: "loss" type: "Softmax" bottom: "rs" top: "loss"}
        """)
    f.close()
    return f.name

if __name__=='__main__':
    df = create_deploy()
    Net = caffe.Net(df,caffe.TEST)

    Net.blobs['lv'] = np.random.randn(1,1,12,202)
    Net.blobs['lv_upper'] = np.random.randn(1,1,12,202)

    out = Net.forward()

    out.keys()

    out['conv'].shape

    out['pool'].shape

    out['ip2'].shape


code: from torchvision.models import resnet


code:
# 检查特征提取
import h5py
import numpy as np
import os
import pickle

from Config.Config import DPN107_RGB_200_PATH
from Config.Config import TSNscore_FEATURE_PATH as FEATURE
from Config.Config import FRAME_COUNT_PKL

from FeatureExtruct.DPN107_RGB.RGB_SinglePicture_Dataset import RGB_Single_Frame_Dataset

TTT = 'AA6nXQMyfhU,fG0nn2IVdDM,K5wPwCFVkhU,pElJ7U46XnQ,Uofnmkfohkc,ZTwmb1d44bc,aAY_M6M26TI,fgBFlwM466w,k7nuduqYOdQ,\
Pfc7KbwqdYk,UoGoOznvKew,zuNQFkkyBNo,aB5xErksFkI,fgP2pf2rh4Q,K7oJNZsI1Cc,PG0ao4HkF8M,uOk4EFDsDP4,zUt53fHpqc8'
TTT = TTT.split(',')

vids = TTT

# TCN feature
def load_TSNscore_from_file(vid):
    with h5py.File(FEATURE + '/feat/%s.h5' % vid, 'r') as hf:
        fg = np.asarray(hf['fg'])
        bg = np.asarray(hf['bg'])
        feat1 = np.hstack([fg,bg])
    with h5py.File(FEATURE + '/flow/%s.h5' % vid, 'r') as hf:
        fg2 = np.asarray(hf['fg']) # 200
        bg2 = np.asarray(hf['bg']) # 2
        feat2 = np.hstack([fg2,bg2])
    feat = feat1 + feat2
    return feat

# dpn107 201 feature
def load_DPN107_from_file(vid):
    try:
        with h5py.File(os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid)),'r') as f:
            d = f['feature201'][:]
            return d
    except Exception as E:
        raise E

# frame len
with open(FRAME_COUNT_PKL,'rb') as f:
    frame_count = pickle.load(f)

keys = 'rhOtqArO-3Y'

frame_num = frame_count[keys]
tsn_feature = load_TSNscore_from_file(keys)
dpn_feature = load_DPN107_from_file(keys)

tsn_feature.shape
dpn_feature.shape

# dataset = RGB_Single_Frame_Dataset('training')
# gen = dataset.enum_all_picture()
#
# for i,X in enumerate(gen):
#     print(i,X[0],len(X[2]))

import matplotlib.pyplot as plt

for i in range(201):
    plt.clf()
    # plt.plot(tsn_feature[:,i])
    plt.plot(dpn_feature[:,i])
    plt.ylim(0,1)
    # plt.show()
    # plt.savefig('/tmp/PICTURE/TSN/tsn_{}.jpg'.format(i))
    plt.savefig('/tmp/PICTURE/DPN/dpn_{:05d}.jpg'.format(i))

from Config.Config import ACTNET200V13_PKL
import pickle
with open(ACTNET200V13_PKL,'rb') as f:
    d = pickle.load(f)
database = d['database']
database[keys]

plt.plot(tsn_feature[:,200])
plt.show()

plt.plot(tsn_feature[:,201])
plt.show()


code:
from Config.Config import TSNscore_FEATURE_PATH
import numpy as np

from DataSet.Dataset_TSNscore_Class_nobi import Dataset_TSNscore_Class_nobi

dataset = Dataset_TSNscore_Class_nobi('validation','train')

X,Y = dataset.nextbatch()

X.shape

x0 = X[:,0,12:,:]

xx = np.random.normal(0,1,(3,3))
xx

np.max(x0)
np.min(x0)
np.mean(x0)
np.var(x0)
np.median(x0)


code:
import torch
import torch.nn as nn
from torch.autograd import Variable

import json
import os
import pickle
import h5py
import numpy as np
from Tools.eval_detection import ANETdetection

from Model.origin_tcn import TCN_Class2
from Model.sptt import SSN_OnlyClass

from Tools.utils import sliding_window_aggregation_func,gen_json,nms_all
from Config.Config import ACTNET200V13_PKL,TSNscore_FEATURE_PATH,TEST_PROPOSALS_PKL,TRAIN_PROPOSALS_PKL,VAL_PROPOSALS_PKL,ACTNET200V13_JSON

from DataSet.Dataset_TSNscore_Class import Dataset_TSNscore_Class
from DataSet.Dataset_TSNscore_Class_nobi import Dataset_TSNscore_Class_nobi

from DataSet.Tools_TSNscore import load_TSNscore_from_file,extract_TSN_feature
from DataSet.Utils import mulity_scale_pairs

def classify_video(gt, feature_path='/home/DATASETS/actnet/tsn_score', subset='validation'):

    score_per_video = {}
    cnt = 0
    for vid, vitem in gt.items():
        if vitem['subset'] != subset or not os.path.exists(feature_path + '/feat/%s.h5' % vid):
            continue

        with h5py.File(feature_path + '/feat/%s.h5' % vid, 'r') as hf:
            fg = np.asarray(hf['fg'])
        with h5py.File(feature_path + '/flow/%s.h5' % vid, 'r') as hf:
            fg += np.asarray(hf['fg'])

        model_scores = sliding_window_aggregation_func(fg[:,np.newaxis,:], norm=False)
        model_scores = np.hstack((0, model_scores))
        score_per_video[vid] = model_scores

        if cnt % 100 == 0:
            print('classify video {}...'.format(cnt))
        cnt += 1

    return score_per_video

##########################################################3

SAVE_DIR                  = '/mnt/md1/Experiments/PYTSN_Test7'
# RANKED_PROPOSALS_PKL      = '/mnt/md1/Experiments/PYTSN_Test2/VAL_BACKUP/ranked_proposals.pkl'
RANKED_PROPOSALS_PKL      = '/mnt/md1/Experiments/PYTSN_Test7/ranked_proposals.pkl'
# SUBSET                    = 'validation'
SUBSET                    = 'testing'
# CKPT                      = '/mnt/md1/Experiments/PYTSN_Test7/tsnscore_ssn_class2_model_55.ckpt'
CKPT                      = '/mnt/md1/Experiments/PYTSN_Test2/class2_model_11.ckpt'
OPEN_NMS_FOR_RANKED_PROPOSAL = True
OPEN_NMS_FOR_CLASS_PROPOSAL  = True

FEATURE = TSNscore_FEATURE_PATH

##########################################################3

with open(os.path.join(SAVE_DIR,RANKED_PROPOSALS_PKL),'rb') as f:
    ranked_proposals = pickle.load(f)

if OPEN_NMS_FOR_RANKED_PROPOSAL:
    ranked_proposals = nms_all(ranked_proposals, topK=20, nms_thor=0.45)

with open(ACTNET200V13_PKL, 'rb') as f:
    gt = pickle.load(f)
    names = gt['actionIDs']
    gt = gt['database']

with open(TRAIN_PROPOSALS_PKL, 'rb') as f:
    train_proposals = pickle.load(f)

model = TCN_Class2().cuda()
# model = SSN_OnlyClass(featuredim=202,seg_split=[3,9,12]).cuda()

model = nn.DataParallel(model)
model.load_state_dict(torch.load(CKPT))
print('Load model successfully!')
model.eval()

SoftMaxFunction = nn.Softmax(dim=-1).cuda()

score_tsn = classify_video(gt, subset=SUBSET, feature_path=FEATURE)
with open(os.path.join(SAVE_DIR,'score_tcn.pkl'),'wb') as f:
    pickle.dump(score_tsn,f)
# with open(os.path.join(SAVE_DIR,'score_tcn.pkl'),'rb') as f:
#     score_tsn = pickle.load(f)

priors = [score_tsn]
weights = [1]

cnt = 0
lastvid = None
classified_proposals = dict()

for vid,proposal in ranked_proposals.items():

    if not os.path.exists( FEATURE + '/feat/%s.h5' % vid) or len(proposal)==0:
        continue
    feat = load_TSNscore_from_file(vid)

    num_frame = gt[vid]['numf']
    duration = gt[vid]['duration']
    proposal = np.asarray(proposal)
    classified_proposals[vid] = []

    for i in range(proposal.shape[0]):

        current = np.copy(proposal[i,...])
        current[3] = 0

        # TCN
        # x = extract_TSN_feature(feat, current[np.newaxis,:], num_frame, duration, PYRAMID=[100000], repeat=False)
        x = 1

        # sptt
        # current_expand = current[np.newaxis,:]
        # current_expand = mulity_scale_pairs(current_expand)[0]
        # x = extract_TSN_feature(feat, current_expand, num_frame, duration, PYRAMID=[12,12])

        if x is not None:

            # Origin TCN
            ######################################
            # x = np.dot(x.T,x).flatten()
            # x = Variable(torch.from_numpy(x)).cuda().float()
            # x = x.view(1,-1)
            # prob = model(x)
            # prob = prob.cpu().data.numpy()
            prob = np.zeros((1,201))

            # sptt
            ######################################

            # x = x[np.newaxis,:]
            # x = Variable(torch.from_numpy(x[:,12:,:])).cuda().float()
            # prob = model(x)
            # prob = prob.cpu().data.numpy()

            for prior, weight in zip(priors, weights):
                prior = prior[vid].reshape(1,201) * weight
                prob += prior

            pred = prob.argmax(1)
            current[3] = pred

        # print current
        classified_proposals[vid].append(current)

    if cnt%100 == 0 :
        print('evaling {} ... '.format(cnt))
    cnt += 1

with open(os.path.join(SAVE_DIR,'classified_proposals.pkl'),'wb') as f:
    pickle.dump(classified_proposals,f)

# with open(os.path.join(SAVE_DIR,'classified_proposals.pkl'),'rb') as f:
#     classified_proposals=pickle.load(f)

if OPEN_NMS_FOR_CLASS_PROPOSAL:
    classified_proposals = nms_all(classified_proposals, topK=20, nms_thor=1, remove_background=True)

# test on offical evaluation code
id2name = {}
for name, ids in names.items():
    id2name[ids['class']] = name
output = gen_json(classified_proposals, id2name)
PREDICT_JSON = os.path.join(SAVE_DIR,'prediction.json')
with open(PREDICT_JSON, 'w') as f:
    json.dump(output, f)

if SUBSET == 'validation':
    eval1 = ANETdetection(ACTNET200V13_JSON, PREDICT_JSON, subset='validation', tiou_thr=0.5, verbose=True, check_status=False)
    eval1.evaluate()
    eval2 = ANETdetection(ACTNET200V13_JSON, PREDICT_JSON, subset='validation', tiou_thr=0.75, verbose=True, check_status=False)
    eval2.evaluate()
    eval3 = ANETdetection(ACTNET200V13_JSON, PREDICT_JSON, subset='validation', tiou_thr=0.95, verbose=True, check_status=False)
    eval3.evaluate()


code:
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import logging
import os
import pickle

from Config.Config import ACTNET200V13_PKL
from DataSet.Dataset_TSNscore_Pair import Dataset_TSNscore_Pair
from Tools.utils import nms, recall_vs_iou_thresholds, get_gt, convert
from Model.origin_tcn import TCN_Pair2
from Model.sptt import SSN_OnlyPair

SAVE_DIR = '/mnt/md1/Experiments/PYTSN_Test7/'
CKPT = '/mnt/md1/Experiments/PYTSN_Test7/tsnscore_ssn_pair2_model_23.ckpt'
# SUBSET = 'validation'
SUBSET = 'testing'

# model = TCN_Pair2().cuda()
model = SSN_OnlyPair(featuredim=202,seg_split=[3,9,12]).cuda()

model = nn.DataParallel(model)
model.load_state_dict(torch.load(CKPT))
softmax = nn.Softmax(dim=-1).cuda()

val_dataset = Dataset_TSNscore_Pair(SUBSET, 'test')
gen = val_dataset.enum_genfucntion()

ranked_proposals = dict()
last_vid = None
cnt = 0
for idx,[vid,proposals,feat] in enumerate(gen):

    if len(proposals) == 0:
        continue

    feat = np.expand_dims(feat,1)
    feat = Variable(torch.from_numpy(feat),volatile=True).cuda().float()

    # out = model(feat[:,:,:12,:],feat[:,:,12:,:])
    out = model(feat[:,0,12:,:])

    out = softmax(out)
    prob = out[:,0]

    for i in range(proposals.shape[0]):
        proposals[i,2] = 1 - prob[i]

    if vid not in ranked_proposals.keys():
        ranked_proposals[vid] = []

    ranked_proposals[vid].append(proposals)

    show = False
    if last_vid != vid:
        last_vid = vid
        cnt += 1
        show = True

    if cnt % 100 == 0 and show:
        print('ranked proposal ...',len(ranked_proposals))

with open(os.path.join(SAVE_DIR,'ranked_proposals.pkl'),'wb') as f:
    pickle.dump(ranked_proposals,f)

# with open(os.path.join(SAVE_DIR,'ranked_proposals.pkl'),'rb') as f:
#       ranked_proposals = pickle.load(f)

with open(ACTNET200V13_PKL, 'rb') as f:
    gt = pickle.load(f)['database']

proposal_at_1 = {'s-init':[],'s-end':[],'score':[],'label':[],'video-id':[]}

for vid, proposal in ranked_proposals.items():

    proposal = np.asarray(proposal)
    # 处于未知的原因需要特别判断一下proposal的形状
    if len(proposal.shape) == 3:
        proposal = proposal.reshape((proposal.shape[0]*proposal.shape[1], proposal.shape[2]))
    else:
        proposal = np.concatenate(proposal)

    keep_ind = nms(proposal, proposal[:,2], 0.45)
    proposal = proposal[keep_ind,:]

    proposal_at_1['s-init'].append(proposal[0,0])
    proposal_at_1['s-end'].append(proposal[0,1])
    proposal_at_1['score'].append(proposal[0,2])
    proposal_at_1['video-id'].append('v_'+vid)

if SUBSET == 'validation':
    print('START EVAL ...')
    gt2 = get_gt(gt)
    iou_thrs = np.arange(0.1, 1.0, 0.1)
    recall_at_1 = recall_vs_iou_thresholds(convert(proposal_at_1), gt2, iou_threshold=iou_thrs)
    print(np.array_str(np.vstack([iou_thrs, recall_at_1]), precision=4, suppress_small=True),flush=True)


code:
import numpy as np
import pickle
from Config.Config import ACTNET200V13_PKL
from Tools.utils import nms, recall_vs_iou_thresholds, get_gt, convert

# RANKED_PROPOSALS_FILE = '/mnt/md1/Experiments/PYTSN_Test2/VAL_BACKUP/ranked_proposals.pkl'
RANKED_PROPOSALS_FILE = '/mnt/md1/Experiments/PYTSN_Test7/ranked_proposals.pkl'

print('ranked_file: {}'.format(RANKED_PROPOSALS_FILE))

with open(RANKED_PROPOSALS_FILE,'rb') as f:
    ranked_proposals = pickle.load(f)

with open(ACTNET200V13_PKL, 'rb') as f:
    gt = pickle.load(f)['database']

proposal_at_1 = {'s-init':[],'s-end':[],'score':[],'label':[],'video-id':[]}

for vid, proposal in ranked_proposals.items():

    proposal = np.asarray(proposal)
    # 处于未知的原因需要特别判断一下proposal的形状
    if len(proposal.shape) == 3: proposal = proposal.reshape((proposal.shape[0]*proposal.shape[1], proposal.shape[2]))
    else: proposal = np.concatenate(proposal)

    keep_ind = nms(proposal, proposal[:,2], 0.45)
    proposal = proposal[keep_ind,:]

    proposal_at_1['s-init'].append(proposal[0,0])
    proposal_at_1['s-end'].append(proposal[0,1])
    proposal_at_1['score'].append(proposal[0,2])
    proposal_at_1['video-id'].append('v_'+vid)

gt2 = get_gt(gt)
iou_thrs = np.arange(0.1, 1.0, 0.1)
recall_at_1 = recall_vs_iou_thresholds(convert(proposal_at_1), gt2, iou_threshold=iou_thrs)

print(np.array_str(np.vstack([iou_thrs, recall_at_1]), precision=4, suppress_small=True),flush=True)


code:
# 抽取特征到文件
# pickle: featurelen x 201

from Config.Config import DPN107_RGB_200_PATH

import pickle
import numpy as np
import h5py
import os

import torch
import torch.nn as nn
from torch.autograd import Variable

from FeatureExtruct.DPN107_RGB.RGB_SinglePicture_Dataset import RGB_Single_Frame_Dataset

# laod model

model = torch.load('/mnt/md1/Experiments/DPN_Extruct_200_Test1/raw_dpn107_model.pkl')
model = nn.DataParallel(model).cuda()
softmax = nn.Softmax(dim=-1).cuda()
model.load_state_dict(torch.load('/mnt/md1/Experiments/DPN_Extruct_200_Test1/backup/dpn107_RGB_fineturn_201_model_011.ckpt'))

# open eval model
model = model.eval()
softmax = softmax.eval()

# extruct feature
# def ExtructFeature(gen):
#
#     lastvid = None
#     store_preds = []
#     store_labels = []
#
#     for i,[vid,images,labels] in enumerate(gen):
#
#         if lastvid is None: lastvid = vid
#
#         feature_path = os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid))
#         if os.path.exists(feature_path): continue
#
#         images = Variable(images,volatile=True).cuda()
#         pred = softmax(model(images))
#
#         store_preds.append(pred.data.cpu().numpy())
#         store_labels.extend(labels)
#
#         if vid!=lastvid:
#             store_preds = np.concatenate(store_preds)
#             # save feature
#             with h5py.File(feature_path,'w') as f:
#                 f['feature201'] = store_preds
#                 f['label'] = store_labels
#                 f.attrs['vid'] = vid
#
#             lastvid = vid
#             store_preds = []
#             store_labels = []
#
#     if len(store_labels) != 0 :
#         store_preds = np.concatenate(store_preds)
#         with h5py.File(feature_path,'w') as f:
#             f['feature201'] = store_preds
#             f['labels'] = store_labels
#             f.attrs['vid'] = vid

def ExtructFeature(dataset):

    vids = dataset.vids

    for idx,vid in enumerate(vids):

        feature_path = os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid))
        if os.path.exists(feature_path) == True:
            continue

        if os.path.exists('/mnt/md1/Dataset/ActivityNet/Frames/{}'.format(vid)) == False:
            continue

        try:
            gen = dataset.enum_vid_feature(vid)
        except Exception:
            continue

        store_preds = []
        store_labels = []

        for i,[_,images,labels] in enumerate(gen):
            images = Variable(images,volatile=True).cuda()
            pred = softmax(model(images))
            store_preds.append(pred.data.cpu().numpy())
            store_labels.extend(labels)

        if len(store_preds) == 0: continue
        store_preds = np.concatenate(store_preds)

        with h5py.File(feature_path,'w') as f:
            f['feature201'] = store_preds
            f['label'] = store_labels
            f.attrs['vid'] = vid

        if idx%10==0: print(idx,'...')

# load dataset
# dataset = RGB_Single_Frame_Dataset('training')
# ExtructFeature(dataset)

dataset = RGB_Single_Frame_Dataset('validation')
ExtructFeature(dataset)