.
This section should consider any external factors such as travel fatigue for Deportivo Capiata that could impact their performance levels against Guarani de Fram at home ground advantage.
.
huang-ying-2017/MoCo-v1/moco/losses.py
import torch
import torch.nn.functional as F
def nt_xent_loss(zis,
zjs,
temperature=0.1,
device=torch.device(‘cpu’),
mask=None):
“”” NT_Xent loss function
:param zis: normalized embeddings (batch_size, dim)
:param zjs: normalized embeddings (batch_size, dim)
:param temperature:
:param device:
:param mask:
:return:
“””
# normalize projection feature vectors
# if zis.dim() == 4:
# zis = F.normalize(zis.view(zis.shape[0], zis.shape[1], -1), dim=2)
# zjs = F.normalize(zjs.view(zjs.shape[0], zjs.shape[1], -1), dim=2)
N = zis.shape[0]
if mask is None:
pos_mask = torch.eye(N).to(device)
neg_mask = torch.ones((N,N)).to(device) – pos_mask
pos_mask = pos_mask.repeat(N,1)
neg_mask = neg_mask.repeat(N,1)
mask = torch.cat([pos_mask,neg_mask],dim=0)
mask = torch.cat([mask,pos_mask,neg_mask],dim=1)
sim_matrix = torch.exp(torch.mm(zis,zjs.t().contiguous())/temperature) * mask
sim_sum = sim_matrix.sum(dim=-1)
loss = -(torch.log(sim_matrix.diag()/sim_sum)).sum()/(2*N)
return loss
def nt_xent_loss_torchvision(zis,
zjs,
temperature=0.1,
device=torch.device(‘cpu’),
mask=None):
“”” NT_Xent loss function
:param zis: normalized embeddings (batch_size * num_crops , dim)
:param zjs: normalized embeddings (batch_size * num_crops , dim)
:param temperature:
:param device:
:param mask:
:return:
“””
huang-ying-2017/MoCo-v1/moco/moco.py
import os
import numpy as np
import torch
import torch.nn as nn
from torch.optim import SGD
from torchvision.models import resnet50
from .losses import nt_xent_loss
class MoCo(nn.Module):
def __init__(self,
model_dim=128,
m=0.999,
K=65536,
T=0.07):
super(MoCo,self).__init__()
self.K = K
self.T = T
# create encoder q network
self.encoder_q = resnet50(pretrained=False)
self.encoder_q.fc = nn.Linear(self.encoder_q.fc.in_features,model_dim,bias=False)
# create key encoder network
self.encoder_k = resnet50(pretrained=False)
self.encoder_k.fc = nn.Linear(self.encoder_k.fc.in_features,model_dim,bias=False)
# initialize parameters and copy weight from q network
for param_q,param_k in zip(self.encoder_q.parameters(),self.encoder_k.parameters()):
param_k.data.copy_(param_q.data)
param_k.requires_grad = False
# create queue
self.register_buffer(“queue”,torch.randn(model_dim,K))
self.queue = nn.functional.normalize(self.queue,dim=0)
self.register_buffer(“queue_ptr”,torch.zeros(1,dtype=torch.long))
huang- Ying-2017/MoCo-v1/moco/__init__.py
from .moco import MoCo#include “glut.h”
#include “math.h”
#include “stdlib.h”
#include “stdio.h”
#include “time.h”
#include “vector.h”
#define GL_PI (float)M_PI
// Global variables.
int window;
int width;
int height;
int wireframe_mode;
int line_mode;
int filled_mode;
int shading_mode;
int color_mode;
float eye[4];
float center[4];
float up[4];
float light_pos[4];
float light_amb[4];
float light_diff[4];
float light_spec[4];
// World coordinates.
Vector* wall_list;
Vector* block_list;
// OpenGL objects.
GLuint wall_list_id;
GLuint block_list_id;
// Shaders.
GLuint shader_program_id;
GLuint shader_vertex_id;
GLuint shader_fragment_id;
// Lighting parameters.
GLfloat light_ambient[] = {0.05f, 0.05f, 0.05f};
GLfloat light_diffuse[] = {0.8f, 0.8f, 0.8f};
GLfloat light_specular[] = {1.f, 1.f, 1.f};
GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f};
GLfloat mat_diffuse[] = {0.f, 0.f, 0.f};
GLfloat mat_specular[] = {1.f, 1.f, 1.f};
GLfloat mat_shininess[] = {100.f};
void Init(void);
void RenderScene(void);
void Reshape(int w,int h);
void Display(void);
void Init(void)
{
GLenum err;
wireframe_mode = GL_FALSE;
line_mode = GL_TRUE;
filled_mode = GL_FALSE;
shading_mode = GL_SMOOTH;
color_mode = GL_SMOOTH;
glShadeModel(shading_mode);
glPolygonMode(GL_FRONT_AND_BACK,line_mode?GL_LINE:filled_mode?GL_FILL:GL_POINT);
glClearColor(0.f,0.f,.25f,.25f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glLightfv(GL_LIGHT0,GL_POSITION ,light_pos );
glLightfv(GL_LIGHT0,GL_AMBIENT ,light_amb );
glLightfv(GL_LIGHT0,GL_DIFFUSE ,light_diff);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_spec);
glMaterialfv(GL_FRONT,GL_AMBIENT ,mat_ambient );
glMaterialfv(GL_FRONT,GL_DIFFUSE ,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mesh_shininess);
err = glGetError();
if(err != GL_NO_ERROR) printf(“Init error %dn”,err);
}
void Reshape(int w,int h)
{
width=w;
height=h;
glViewport(0,(height-h)/2,w,h);
}
void RenderScene(void)
{
GLenum err;
glClearDepth(10000.);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.,(float)width/height,.01f,(float)10000.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Vector vec_center(center);
Vector vec_up(up);
Vector vec_eye(eye);
vec_center.Normalize();
vec_up.Normalize();
vec_eye.Normalize();
Vector vec_cross(vec_up.Cross(vec_center));
vec_cross.Normalize();
vec_up.Cross(vec_cross);
float scale_factor=(vec_eye.Dot(vec_cross)+vec_eye.Dot(vec_up))/sqrt(2.f);
vec_cross.Scale(scale_factor);
vec_up.Scale(scale_factor);
Vector vec_right(vec_cross.Cross(vec_center));
gluLookAt(
vec_eye.x(),vec_eye.y(),vec_eye.z(),
vec_center.x(),vec_center.y(),vec_center.z(),
vec_up.x(),vec_up.y(),vec_up.z());
err = glGetError();
if(err != GL_NO_ERROR) printf(“Render error %dn”,err);
if(wireframe_mode==GL_TRUE){
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glColor3f(.75f,.75f,.75f);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,wall_list_id);
glVertexPointer(3,GL_FLOAT,sizeof(float)*wall_list->size(),NULL);
glDrawArrays(GL_QUADS,(GLsizei)wall_list->size()/4,(GLsizei)wall_list->size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,NULL);
glPolygonMode(GL_FRONT_AND_BACK,line_mode?GL_LINE:filled_mode?GL_FILL:GL_POINT);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,block_list_id);
glVertexPointer(3,GL_FLOAT,sizeof(float)*block_list->size(),NULL);
glDrawArrays(GL_TRIANGLES,(GLsizei)block_list->size()/6,(GLsizei)block_list->size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,NULL);
}
else{
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,wall_list_id);
glVertexPointer(3,GL_FLOAT,sizeof(float)*wall_list->size(),NULL);
glDrawArrays(GL_QUADS,(GLsizei)wall_list->size()/4,(GLsizei)wall_list->size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,NULL);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,block_list_id);
glVertexPointer(3,GL_FLOAT,sizeof(float)*block_list->size(),NULL);
glDrawArrays(GL_TRIANGLES,(GLsizei)block_list->size()/6,(GLsizei)block_list->size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,NULL);
}
err = glGetError();
if(err != GL_NO_ERROR) printf(“Render error %dn”,err);
glutSwapBuffers();
}
void Display(void)
{
RenderScene();
}
int main(int argc,char** argv)
{
int i;
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(width,height);
window=glutCreateWindow(“Minecraft”);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
Init();
wall_list=new Vector();
block_list=new Vector();
srand(time(NULL));
for(i=-30;i<=30;i++){
for(int j=-30;jpush_back(Vector(i,j,-30));
block_list->push_back(Vector(i,j,-29));
block_list->push_back(Vector(i,j,-29));
block_list->